有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的代码怎么了?单击按钮时,textlabel和textfield不在同一位置

我正在用Oracle做一些GUI,但我在这里面临一个问题。当我按下大型机中的“add”(添加)按钮时,我称之为“window”(窗口),JLabel和texfield的位置不在它们应该在的位置。我做错了什么? 我试图寻找答案,但没有任何结果

我忘了说JLabel“name”和JTextField“nameText”集中在第二个框架上,不像我希望的那样-查看下面的代码注释“这是我尝试添加的内容,没有任何结果…”“还有这个。”-

如果你能帮助我,我会很高兴。如果问题含糊不清,我向你道歉。谢谢大家!

代码如下:

public class Test extends JFrame implements ActionListener {
//---------------------------------------------------
    private JPanel windowPanel;
    private JLabel welcome;
    private JButton addButton;

    private JButton removeButton;
    private JButton exitButton;
//---------------------------------------------------
    private JPanel addPanel;
    private JLabel name;
    private JTextField nameText;
    private JLabel surName;
    private JTextField surNameText;
    private JLabel idNumber;
    private JLabel idNumberText;
    private JButton addPerson;
    private JButton exitButton1;
//---------------------------------------------------
    private JPanel removePanel;
    private JLabel idNumberRemove;
    private JTextField numberText;
    private JButton removePerson;
    private JButton exit2Button2;

    public Test(){

        setLayout(null);
        windowPanel = new JPanel();
        welcome = new JLabel("Welcome User!");
        welcome.setBounds(150,0, 87, 200);
        welcome.setVisible(true);
        windowPanel.add(welcome);
        add(welcome);
        add(windowPanel);
//---------------------------------------------------
        addButton = new JButton("Add");
        addButton.setBounds(50, 250, 80, 30);
        addButton.setVisible(true);
        windowPanel.add(addButton);
        add(addButton);
        addButton.addActionListener(this);
//---------------------------------------------------
        removeButton = new JButton("Remove");
        removeButton.setBounds(275, 250, 80, 30);
        removeButton.setVisible(true);
        windowPanel.add(removeButton);
        add(removeButton);
        removeButton.addActionListener(this);
//---------------------------------------------------
        exitButton = new JButton("Exit");
        exitButton.setBounds(160, 350, 80, 30);
        exitButton.setVisible(true);
        windowPanel.add(exitButton);
        add(exitButton);
        exitButton.addActionListener(this);
//-------------This is what I've tried to add without any results...--------------------------------------
        setLayout(null);
        addPanel = new JPanel();
        name = new JLabel("First Name:");
        name.setBounds(50, 100, 90, 50);
        name.setVisible(true);
        addPanel.add(name);
//---------------and this.------------------------------------
        nameText = new JTextField(8);
        nameText.setBounds(100, 50, 90, 50);
        nameText.setVisible(true);
        addPanel.add(nameText);
//----------------Not important right now-----------------------------------
        surName = new JLabel("Last Name:");
        surName.setBounds(50, 250, 90, 50);
        surName.setVisible(true);
        addPanel.add(surName);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addButton) {
            JFrame frame1 = new JFrame("Add a person");
            frame1.setVisible(true);
            frame1.setSize(300, 400);
            frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame1.setResizable(true);
            frame1.setLocationRelativeTo(null);
            JPanel panel1 = new JPanel();
            frame1.add(panel1);
            panel1.add(addPanel);

        }
    }
}

共 (0) 个答案