gpt4 book ai didi

java - 如何将内部框架添加到菜单

转载 作者:行者123 更新时间:2023-12-02 04:14:44 26 4
gpt4 key购买 nike

我有一个 GUI,有 3 个菜单:读取文件、添加、完成。具体来说,在“添加”菜单下,我有 3 个菜单项,称为“添加所有者”、“添加住宅属性(property)”和“添加商业属性(property)”。当单击任何这些菜单项时,我会出现一个内部框架,但我不希望所有菜单项的内部框架都相同。与业主相比,我正在尝试为商业和住宅属性(property)添加更多文本字段。所以我不希望每个菜单项的内部框架相同但唯一,这就是我遇到麻烦的地方。这是代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class PropertyGUITest
{
public static void main(String[] args)
{
PropertyGUI obj = new PropertyGUI();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(400,300);
obj.setVisible(true);
obj.setLocation(100,50);
}
}

class PropertyGUI extends JFrame
{
private int countFrames = 0;
public PropertyGUI()
{
super("Property GUI");

JMenuBar bar = new JMenuBar();
setJMenuBar(bar);

JMenu readMenu = new JMenu("Read Files");
bar.add(readMenu);

JMenu addMenu = new JMenu("Add");
bar.add(addMenu);

JMenuItem newFrame1=new JMenuItem("Add Owner");
addMenu.add(newFrame1);

JMenuItem newFrame2=new JMenuItem("Add Residential Property");
addMenu.add(newFrame2);

JMenuItem newFrame3=new JMenuItem("Add Commercial Property");
addMenu.add(newFrame3);

JMenu finishMenu = new JMenu("Finish");
bar.add(finishMenu);

JDesktopPane theDesktop = new JDesktopPane();
add(theDesktop);

JMenuItem writeItem = new JMenuItem("Write Owners");
finishMenu.add(writeItem);

JMenuItem readpItem = new JMenuItem("Read Properties");
readMenu.add(readpItem);

JMenuItem readoItem = new JMenuItem("Read Owners");
readMenu.add(readoItem);

JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//System.exit(0);
dispose();
}
}
);
finishMenu.add(exitItem);

newFrame1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
countFrames++;
JInternalFrame jf = new JInternalFrame("Add Owner",true,true,true,true);
theDesktop.add(jf);
jf.setVisible(true);
jf.pack();
jf.setSize(300,200);
jf.setLocation(countFrames*10,countFrames*20);

CustomPanel panel1 = new CustomPanel();
jf.add(panel1);


}
}
);

newFrame2.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
countFrames++;
JInternalFrame jf = new JInternalFrame("Add Residential Property",true,true,true,true);
theDesktop.add(jf);
jf.setVisible(true);
jf.pack();
jf.setSize(300,200);
jf.setLocation(countFrames*10,countFrames*20);

CustomPanel panel2 = new CustomPanel();
jf.add(panel2);


}
}
);

newFrame3.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
countFrames++;
JInternalFrame jf = new JInternalFrame("Add Commercial Property",true,true,true,true);
theDesktop.add(jf);
jf.setVisible(true);
jf.pack();
jf.setSize(300,200);
jf.setLocation(countFrames*10,countFrames*20);

CustomPanel panel3 = new CustomPanel();
jf.add(panel3);


}
}
);
}

class CustomPanel extends JPanel
{
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;
JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;
JButton button1;


public CustomPanel()
{
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);

add(new JLabel("Name"), gbc);
gbc.gridy++;
add(new JLabel("Street"), gbc);
gbc.gridy++;
add(new JLabel("City"), gbc);
gbc.gridy++;
add(new JLabel("State"), gbc);
gbc.gridy++;
add(new JLabel("Zip"), gbc);
gbc.gridy++;
add(new JLabel("Submit when done"), gbc);

JTextField[] fields = new JTextField[5];
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add((fields[0] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[1] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[2] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[3] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[4] = new JTextField(10)), gbc);
gbc.gridy++;
JButton btn = new JButton("Submit");
add(btn, gbc);



}
}
}

对于商业和住宅属性(property),我只想在提交按钮之前添加两个用于帐号和价格的文本字段。

最佳答案

i do not qant the internal frame to be the same but unique for each menu item and that is where i am having trouble.

CustomPanel panel1 = new CustomPanel();
...
CustomPanel panel2 = new CustomPanel();
...
CustomPanel panel3 = new CustomPanel();

那么您就不能使用同一个面板。

您需要 3 个不同的面板。您需要:

  1. “所有者面板”
  2. “住宅面板”
  3. “商业面板”

如果每个面板的数据不同,请勿尝试让内部框架使用相同的面板。

//CustomPanel panel1 = new CustomPanel();
OwnerPanel panel1 = new OwnerPanel();
...
//CustomPanel panel2 = new CustomPanel();
ResidentialPanel panel2 = new ResidentialPanel();
...
//CustomPanel panel3 = new CustomPanel();
CommercialPanel panel3 = new CommercialPanel();

关于java - 如何将内部框架添加到菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com