【发布时间】:2014-07-23 10:25:19
【问题描述】:
我正在尝试构建一个带有 2 个按钮的主 GUI 的小程序。一个按钮关闭程序,另一个我想打开一个新的 JPanel,它将包含文本字段等。
我希望能够使按钮看起来像我猜的普通应用程序按钮,漂亮和方形,大小相等等等,但我不知道该怎么做。
另外,我不确定如何通过单击按钮打开新的 JFrame。
界面代码:
package practice;
public class UserInterface extends JFrame {
private JButton openReportSelection = new JButton("Open new Window");
private JButton closeButton = new JButton("Close Program");
private JButton getCloseButton() {
return closeButton;
}
private JButton getOpenReportSelection() {
return openReportSelection;
}
public UserInterface() {
mainInterface();
}
private void mainInterface() {
setTitle("Program Information Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel(new GridLayout(0, 3));
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
getCloseButton().addActionListener(new Listener());
add(centerPanel, BorderLayout.CENTER);
setSize(1000, 200);
setVisible(true);
}
private void addReportPanel() {
JPanel reportPanel = createNewPanel();
getContentPane().add(reportPanel, BorderLayout.CENTER);
}
private JPanel createNewPanel() {
JPanel localJPanel = new JPanel();
localJPanel.setLayout(new FlowLayout());
return localJPanel;
}
}
ActionListener 类代码:
package practice;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Listener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
}
编辑:我认为打开一个新的 JPanel 比 JFrame 更好。通过点击 Jbutton 来执行此操作的最佳方法是什么?
【问题讨论】:
-
使用combinations of layout managers,以及white space的布局内边距和边框。
-
你的意思是要在给定的
JFrame中打开一个新的JPanel?
标签: java swing user-interface netbeans jpanel