我正在创建一个桌面应用,但我无法弄清楚为什么JFrame正在通过JInternalFrame流出(见图)。我已经尝试在下面添加相关代码,因此请忽略遗漏{},它在实际应用程序中运行时没有错误。
public class MainJFrame extends JFrame {
private static final long serialVersionUID = 2377310559170663631L;
Container cPane;
JDesktopPane desktopPane = new JDesktopPane();
JPanel deskPanel;
JButton jButtonCreateWLog, jButtonCreateCLog;
public static void main(String args[]) {
MainJFrame w = new MainJFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int maxX = screenSize.width - 100;
int maxY = screenSize.height - 200;
w.setPreferredSize(new Dimension(maxX, maxY));
w.setSize(maxX, maxY);
w.setTitle("Estes Fitness Help");
w.setVisible(true);
}
public MainJFrame() {
super("Estes Fitness Help");
getContentPane().add(desktopPane);
cPane = getContentPane();
cPane.setLayout(null);
initiateComponents();
initiateMenuBar();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initiateComponents(){
//Set size to match desktop size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int maxX = screenSize.width - 100;
int maxY = screenSize.height - 200;
//Create panel inside desktop
deskPanel = new JPanel();
deskPanel.setName("Weight Log Entry");
deskPanel.setLayout(null);
deskPanel.setBackground(new Color(200, 50, 50));
//button for creating a weight log
jButtonCreateWLog = new JButton();
jButtonCreateWLog.setText("Create Weight Log");
jButtonCreateWLog.setBounds(80, 120, 150, 25);
jButtonCreateWLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCreateWLogActionPerformed(e);
}
});
//button for creating a cardio log
jButtonCreateCLog = new JButton();
jButtonCreateCLog.setText("Create Cardio Log");
jButtonCreateCLog.setBounds(250, 120, 150, 25);
jButtonCreateCLog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonCreateCLogActionPerformed(e);
}
});
//add components to deskPanel
deskPanel.add(jButtonCreateWLog);
deskPanel.add(jButtonCreateCLog);
deskPanel.setSize(maxX, maxY);
cPane.add(deskPanel);
pack();
setVisible(true);
}
private void jButtonCreateWLogActionPerformed(ActionEvent e){
MainJFrameController desktopPaneC = new MainJFrameController(this);
desktopPaneC.actionPerformed(e);
}
这是MainJFrameController:
public class MainJFrameController implements ActionListener{
private MainJFrame desktopFrame;
public MainJFrameController(){
}
public MainJFrameController(MainJFrame desktopFrame){
this.desktopFrame = desktopFrame;
desktopFrame.getCreateWLogButton().addActionListener(this);
desktopFrame.getCreateCLogButton().addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equalsIgnoreCase("Create Weight Log")){
getCreateWLogButton_actionPerformed(e);
} else if (action.equalsIgnoreCase("Create Cardio Log")){
getCreateCLogButton_actionPerformed(e);
}
}
private void getCreateWLogButton_actionPerformed(ActionEvent e) {
StoreWeightLogJFrame SWLJFrame = new StoreWeightLogJFrame();
SWLJFrame.pack();
SWLJFrame.setVisible(true);
desktopFrame.add(SWLJFrame);
}
}
这是WeightLog JInternalFrame
public class StoreWeightLogJFrame extends JInternalFrame{
private static final long serialVersionUID = 7750452209025354283L;
JPanel jPanel1;
JLabel jLabelSets, jLabelReps, jLabelWeight;
JTextField jTextFieldSets, jTextFieldReps, jTextFieldWeight;
JButton jButtonSaveWLog;
public StoreWeightLogJFrame() {
super("Store Weight Log");
initiateComponents();
this.add(jPanel1);
this.setBounds(0, 0, 505, 505);
this.setVisible(true);
this.pack();
this.setClosable(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
System.out.println("StoreWeightLogJFrame initialized");
}
private void initiateComponents(){
jPanel1 = new JPanel();
jLabelSets = new JLabel();
jTextFieldSets = new JTextField();
jLabelReps = new JLabel();
jTextFieldReps = new JTextField();
jLabelWeight = new JLabel();
jTextFieldWeight = new JTextField();
jButtonSaveWLog = new JButton();
jPanel1.setName("Weight Log Entry");
jPanel1.setLayout(null);
jPanel1.setBackground(new Color(255, 50, 50));
jLabelSets.setText("Sets: ");
jLabelSets.setBounds(25, 25, 100, 50);
jTextFieldSets.setText("3");
jTextFieldSets.setBounds(80, 40, 25, 25);
jLabelReps.setText("Reps: ");
jLabelReps.setBounds(25, 60, 100, 50);
jTextFieldReps.setText("10");
jTextFieldReps.setBounds(80, 75, 25, 25);
jLabelWeight.setText("Weight: ");
jLabelWeight.setBounds(25, 110, 50, 25);
jTextFieldWeight.setText("225");
jTextFieldWeight.setBounds(80, 110, 30, 25);
jButtonSaveWLog.setText("Save Log");
jButtonSaveWLog.setBounds(40, 150, 100, 25);
jButtonSaveWLog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButtonSaveWLogActionPerformed(e);
}
});
jPanel1.add(jLabelSets);
jPanel1.add(jTextFieldSets);
jPanel1.add(jLabelReps);
jPanel1.add(jTextFieldReps);
jPanel1.add(jLabelWeight);
jPanel1.add(jTextFieldWeight);
jPanel1.add(jButtonSaveWLog);
jPanel1.setBounds(5, 5, 500, 550);
}
答案 0 :(得分:0)
getContentPane().add(desktopPane);
cPane = getContentPane();
首先,将桌面窗格添加到内容窗格中。
cPane.add(deskPanel);
然后您将deskPanel添加到内容窗格。
因为您使用的是null布局,所以您已将两个组件添加到同一个容器中,当鼠标悬停在该容器上时,将显示该按钮。
不知道你到底想要做什么,但我猜你应该这样做:
所以基本逻辑就像:
JDesktopPane desktop = new JDesktopPane();
frame.add(desktop, BorderLayout.CENTER);
现在,如果您希望“按钮”出现在框架上,您可以为按钮创建另一个面板:
JPanel buttonPanel = new JPanel();
buttonPanel.add(theFirstButton);
buttonPanel.add(theSecondButton);
frame.add(buttonPanel, BorderLayout.PAGE_START);
现在框架将包含一个面板,框架顶部有按钮,桌面窗格将位于此面板下方。
阅读Layout Managers上Swing教程中的部分,了解更多信息和工作示例。