GUI不添加面板

时间:2015-09-24 02:26:06

标签: java swing

初学者使用GUI,无法弄清楚为什么这不会在我的GUI中添加字段。它会打开框架,但它是空的。也没有标记错误。任何帮助表示赞赏。

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

public class Project3 extends JFrame {

    public Project3() {

        //set layout
        setLayout(new GridLayout(5, 2, 5, 5));

        //add radio panel
        JPanel radioPanel = new JPanel();

        //set Radio Buttons
        JRadioButton iter = new JRadioButton("Iteration", true);
        JRadioButton recur = new JRadioButton("Recursion", true);

        //add radio buttons to group
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(iter);
        radioGroup.add(recur);
        radioPanel.add(iter);
        radioPanel.add(recur);
        recur.setSelected(true);

        //add another panel
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(4, 2, 5, 5));

        //add text fields and button
        JLabel enter = new JLabel("Enter n:");
        JTextField eight = new JTextField(8);
        //?????????????????????????????
        JButton compute = new JButton("Compute");
        JLabel result = new JLabel("Result:");
        JTextField eight1 = new JTextField(8);
        JLabel efficiency = new JLabel("Efficiency:");
        JTextField eight2 = new JTextField(8);

        //add items to panel
        secondPanel.add(enter);
        secondPanel.add(eight);
        secondPanel.add(compute);
        secondPanel.add(result);
        secondPanel.add(eight1);
        secondPanel.add(efficiency);
        secondPanel.add(eight2);

        add(radioPanel);
        add(secondPanel);

    }

    public static void main(String[] args) {

        // Create frame
        JFrame frame = new JFrame("Project 3");
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

3 个答案:

答案 0 :(得分:2)

Project3来自JFrame,但在main方法中,您创建了一个完全独立的JFrame实例,并完全忽略了您的Project3

这只会增加混淆,而是从Project3扩展JPanel,并将其实例添加到新创建的JFrame

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Project3 extends JPanel {

    public Project3() {

        //set layout
        setLayout(new GridLayout(5, 2, 5, 5));

        //add radio panel
        JPanel radioPanel = new JPanel();

        //set Radio Buttons
        JRadioButton iter = new JRadioButton("Iteration", true);
        JRadioButton recur = new JRadioButton("Recursion", true);

        //add radio buttons to group
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(iter);
        radioGroup.add(recur);
        radioPanel.add(iter);
        radioPanel.add(recur);
        recur.setSelected(true);

        //add another panel
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(4, 2, 5, 5));

        //add text fields and button
        JLabel enter = new JLabel("Enter n:");
        JTextField eight = new JTextField(8);
        //?????????????????????????????
        JButton compute = new JButton("Compute");
        JLabel result = new JLabel("Result:");
        JTextField eight1 = new JTextField(8);
        JLabel efficiency = new JLabel("Efficiency:");
        JTextField eight2 = new JTextField(8);

        //add items to panel
        secondPanel.add(enter);
        secondPanel.add(eight);
        secondPanel.add(compute);
        secondPanel.add(result);
        secondPanel.add(eight1);
        secondPanel.add(efficiency);
        secondPanel.add(eight2);

        add(radioPanel);
        add(secondPanel);

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Project 3");
                frame.add(new Project3());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

}

通过这样做,您可以解耦代码并为组件提供更高级别的重复使用,现在您可以将Project3添加到您喜欢的容器中

答案 1 :(得分:1)

您实际上从未创建Project3 - 只是JFrame

Pehaps

// Create frame
JFrame frame = new JFrame("Project 3");

应该是

// Create frame
JFrame frame = new Project3();

答案 2 :(得分:1)

您只需创建一个普通的JFrame。您需要创建您编写的Project3类的实例。

即。

public static void main(String[] args) {

    // Create frame
    Project3 frame = new Project3();
    frame.setTitle("Project 3");
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}