4つのコンポーネントを含む1つのパネルがあります。 1つのJLabel、1つのJButton、1つのJTextFieldと1つのJPopupMenu
メソッド "testMethod()"があります。まずパネルからコンポーネントの数を取得し、コンポーネントのタイプ(JButton、JLabelなど)を取得します。異なるイベントタイプを経由するときのJPanelと異なるコンポーネントの値
- JButtonのクリックイベントを通じて来て、それはパネル上の4つの成分を示し、すべての4つの成分が
- は後に来て取得する場合:制御フローはのtestMethod()になると
は今起こる2つのシナリオがありますJPopupMenuを取得せず、JPopupMenuを取得しない3つのコンポーネントをパネル上に表示します。
この現象の原因は何もありません。私はインターネット上で多くを検索し、JPopupMenuのドキュメントも読んでいましたが、これについては何も見つかりませんでした。続き
コードです:
(それは非常に大きなコードの一部ですので、私はここにシナリオを示しているだけでそのコードを置く)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestProject extends JFrame {
JPanel panel;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestProject frame = new TestProject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestProject() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testMethod();
}
});
panel.add(btnNewButton);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
// Creating upper level container
JPopupMenu editMenu = new JPopupMenu("Edit");
JMenuItem item1 = new JMenuItem("Item 1");
item1.setBackground(Color.WHITE);
JMenuItem item2 = new JMenuItem("Item 2");
item2.setBackground(Color.WHITE);
item2.setBackground(Color.WHITE);
// Setting Copy action listener
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testMethod();
}
});
// Adding the menu items on the popup menu
editMenu.add(item1);
editMenu.add(item2);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Added check to open menu only on left click
if (e.getButton() == MouseEvent.BUTTON3)
editMenu.show(panel, e.getX(), e.getY());
}
});
// Adding popup menu to the logAreaPanel
panel.add(editMenu);
}
void testMethod()
{
// Here getting number of components of panel
// When coming through JButton click event, it shows 4 components on panel and after get all 4 components
int items = panel.getComponentCount();
Component c;
// Getting all the trees one by one and storing their data into StringBuilder
for(int loopCounter = 0; loopCounter < items; loopCounter++)
{
// here getting different component types
// When coming through JMenuItem click event, it shows 3 components on panel(not getting JPopupMenu) and after only 3 components(not getting JPopupMenu)
c = panel.getComponent(loopCounter);
}
}
}
任意の助けいただければ幸いです。 :-)
ポップアップメニューが表示されたら、ボタンまたはポップアップメニューで3つのコンポーネントしか取得できません。ポップアップメニューを表示すると、jpanelには表示されなくなるという問題があるようです。 – matt
こんにちは@mattは返信をありがとう。私にも同じように見えますが、私が何か間違っているのか、それともこの方法であるのかにかかわらず、何か具体的なものが欲しいです。 – Aman
ポップアップをパネルに追加する必要はありません。[tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup)マウス・リスナーとポップアップを実行します。あなたのためにマウスリスナーを自動的に処理するように思われるので、間違っているかどうかわかりません。このチュートリアルを読むと、ポップアップには独自のウィンドウが表示されていることがわかります。これは、表示されると他のコンポーネントから削除される理由です。 – matt