我正在制定时间表预订应用程序,当我运行项目时它显示组件(总框架),但是我希望当按下按钮重新加载时,组件应该是吐出的。(两个框架,其中一个在其他之下)通过溢出)???
答案 0 :(得分:0)
根据您的描述,我认为您需要在点击按钮时在框架中添加splitpane,或者您已经在框架中添加了splitpane,并且想要在单击按钮时添加面板。
对于第一个选项,您可以执行以下操作:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
pane.add(jPanel);
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
frame.remove(jPanel);
frame.add(pane);
frame.validate();
}
});
frame.add(jPanel);
frame.setVisible(true);
如果你迟到了,那就试试这个:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
frame.add(pane);
pane.setEnabled(false); // stop user from clicking on divider of split pane.
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
pane.add(jPanel);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
pane.setEnabled(true); // let user change divider location.
}
});
frame.setVisible(true);