public class Question {
int count = 0;
ArrayList<JTextField>[] jt;
public Question() {
final JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JMenuBar mnuBar = new JMenuBar();
JMenu mnu1 = new JMenu("ثبت");
mnu1.setMnemonic(KeyEvent.VK_E);
JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
count = 0;
jt = new ArrayList[4];
for (int i = 0; i < 4; i++)
jt[i] = new ArrayList<JTextField>();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setLayout(new BoxLayout(panel, 1));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("kjdh"));
}
JScrollPane scrollPane = new JScrollPane(panel);
f.getContentPane().add(scrollPane);
}
});
mnu1.add(menuItem);
mnuBar.add(mnu1);
f.setJMenuBar(mnuBar);
f.pack();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Question();
}
};
SwingUtilities.invokeLater(r);
}
}
感谢
答案 0 :(得分:1)
但是只要我将menuBar添加到此代码的scrollPane中 又消失了。这是我的新代码:
因为在JScrollPane
上执行任何操作之前,您没有将MenuItem
添加到内容窗格。
JScrollPane
和JPanel
。 JScrollPane
和JPanel
以包含您的JButtons
MenuItem
上执行操作事件时,将按钮添加到JPanel
并在其上调用revalidate()
。validate()/revalidate()
的内容窗格中调用JFrame
。仅在布局发生变化的组件上调用revalidate()
:在您的上下文中,它是包含JButton
的面板。 repaint()
。所以,你的完整解决方案将成为:
class Question {
int count = 0;
ArrayList<JTextField>[] jt;
JPanel buttonPanel;;
JScrollPane scrollPane;
public Question() {
final JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JMenuBar mnuBar = new JMenuBar();
JMenu mnu1 = new JMenu("ثبت");
mnu1.setMnemonic(KeyEvent.VK_E);
buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red));
buttonPanel.setLayout(new BoxLayout(buttonPanel, 1));
scrollPane = new JScrollPane(buttonPanel);
f.getContentPane().add(scrollPane);
JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
count = 0;
jt = new ArrayList[4];
for (int i = 0; i < 4; i++)
jt[i] = new ArrayList<JTextField>();
for (int i = 0; i < 100; i++) {
buttonPanel.add(new JButton("kjdh"));
}
buttonPanel.revalidate(); // calling revalidate
buttonPanel.repaint();// calling repaint
}
});
mnu1.add(menuItem);
mnuBar.add(mnu1);
f.setJMenuBar(mnuBar);
f.pack();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Question();
}
};
SwingUtilities.invokeLater(r);
}
}
答案 1 :(得分:0)
在f.revalidate();
添加ActionListener
。
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
.................
JScrollPane scrollPane = new JScrollPane(panel);
f.getContentPane().add(scrollPane);
f.revalidate(); // Add this method with JFrame
}
});