【问题标题】:Java tabs in GUIGUI 中的 Java 选项卡
【发布时间】:2010-08-11 05:02:19
【问题描述】:

我希望有人能用勺子喂我这个解决方案。它是我在课堂上的主要实验室的一部分,它并没有给我太多,因为我只是不明白如何用标签制作 GUI。我可以用某种 GUI 制作一个常规程序,但我一直在搜索和阅读,由于整个类部分和声明私有变量,我不能放 2 和 2。所以我要问的是,是否有人可以让我成为一个带有 5 个选项卡的主 GUI,并将我的代码放入 1 个选项卡中,这样我就可以学习并将其余代码放入其他 4 个选项卡中。所以我希望你不要认为我希望你在我有代码时给我代码,我只是没有真正得到标签,而且我们还没有在课堂上讨论过它。这是带有自己 gui 的代码,我希望我输入的内容有意义。我通过看来学习代码,这对我有很大帮助。

  package landscape;
import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Landscape extends JFrame {

    private JFrame mainFrame;
    private JButton calculateButton;
    private JButton exitButton;
    private JTextField lengthField;
    private JLabel lengthLabel;
    private JTextField widthField;
    private JLabel widthLabel;
    private JTextField depthField;
    private JLabel depthLabel;
    private JTextField volumeField;
    private JLabel volumeLabel;
    private JRadioButton builtIn;
    private JRadioButton above;
    private JTabbedPane panel;

    public Landscape()
    {

        JTabbedPane tabs=new JTabbedPane();
        tabs.addTab("Pool", panel);//add a tab for the panel with the title "title"
        //you can add more tabs in the same fashion - obviously you can change the title
        //tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
        mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane
        mainFrame.setSize(265,200);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
        mainFrame.setResizable(false);
        JPanel panel = new JPanel();//FlowLayout is default

        //Pool
            panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
        panel.add(builtIn);
        panel.add(above);
        panel.add(lengthLabel);
        panel.add(lengthField);
        panel.add(widthLabel);
        panel.add(widthField);
        panel.add(depthLabel);
        panel.add(depthField);
        panel.add(volumeLabel);
        panel.add(volumeField);
        panel.add(calculateButton);
        panel.add(exitButton);      
        //creating components
        calculateButton = new JButton ("Calculate");
        exitButton = new JButton ("Exit");
        lengthField = new JTextField (5);
        lengthLabel = new JLabel ("Enter the length of your pool: ");
        widthField = new JTextField (5);
        widthLabel = new JLabel ("Enter the width of your pool: ");
        depthField = new JTextField (5);
        depthLabel = new JLabel ("Enter the depth of your pool: ");
        volumeField = new JTextField (5);
        volumeLabel = new JLabel ("Volume of the pool: ");
        //radio button
        ButtonGroup buttonGroup = new ButtonGroup();
        builtIn = new JRadioButton ("Built in");
        buttonGroup.add(builtIn);
        above = new JRadioButton ("Above");
        buttonGroup.add(above);

        exitButton.setMnemonic('x');
        calculateButton.setMnemonic('C');


        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) 
            { System.exit(0); }
        });

        // create the handlers
        calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object
        calculateButton.addActionListener(chandler); // add event listener

        ExitButtonHandler ehandler = new ExitButtonHandler(); 
        exitButton.addActionListener(ehandler); 

        FocusHandler fhandler = new FocusHandler();
        lengthField.addFocusListener(fhandler);
        widthField.addFocusListener(fhandler);
        depthField.addFocusListener(fhandler);

    }

    class calculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            DecimalFormat num = new DecimalFormat(", ###.##");
            double width;
            double length;
            double depth;
            double volume;
            double volume2;
            double height;
            String instring;

            instring = lengthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                lengthField.setText("0");
            }
            length = Double.parseDouble(instring);

            instring = widthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                widthField.setText("0");
            }
            width = Double.parseDouble(instring);

            instring = depthField.getText();
            if (instring.equals(""))
            {
                instring = "0";
                depthField.setText("0");
            }
            depth = Double.parseDouble(instring);

            volume = width * length * depth;
            volumeField.setText(num.format(volume));

            volume2 = width * length * depth;
            }

        }

    class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    class FocusHandler implements FocusListener
    {
        public void focusGained(FocusEvent e)
        {
            if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)
            {
                volumeField.setText("");
            }
            else if (e.getSource() == volumeField)
            {
                volumeField.setNextFocusableComponent(calculateButton);
                calculateButton.grabFocus();
            }
        }

        public void focusLost1(FocusEvent e)
        {
            if(e.getSource() == widthField)
            {
                widthField.setNextFocusableComponent(calculateButton);
            }
        }

        public void focusLost(FocusEvent e)
        {
            if(e.getSource() == depthField)
            {
                depthField.setNextFocusableComponent(calculateButton);
            }
        }
    }

    public static void main(String args[])
    {
        Landscape app = new Landscape();
    }
}

【问题讨论】:

    标签: java user-interface tabs


    【解决方案1】:

    无需获取内容窗格并将其添加到其中,只需创建一个新的 JPanel 并将您的内容添加到其中即可。然后,将面板添加到新的JTabbedPane 中,使用您想要的任何标题,并将JFrame 的内容窗格设置为选项卡式窗格。

    下面是一个简单的例子:

    JPanel panel=new JPanel();//FlowLayout is default
    panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
    panel.add(builtIn);
    panel.add(above);
    //...you get the picture; add all the stuff you already do, just use panel instead of c
    JTabbedPane tabs=new JTabbedPane();
    tabs.addTab("title", panel);//add a tab for the panel with the title "title"
    //you can add more tabs in the same fashion - obviously you can change the title
    tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
    mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane
    

    您可以保留其余代码的原样,它应该可以正常工作。

    【讨论】:

    • 这很有帮助,谢谢,但是使用 panel.add 时,它会只在选项卡中吗?因为我需要 4 个不同的选项卡,其中包含不同的操作。显然,我需要第一个选项卡包含计算池体积的代码,而我的下一个选项卡将是热水浴缸的代码。另外,我了解如何添加选项卡,但是如何让我的池代码仅显示在名为“标题”的选项卡中?
    • 面板中的任何内容都将显示在名为“标题”的选项卡中,并且仅显示在该选项卡中。要将内容放在其他选项卡中,只需创建另一个 JPanel,将内容放在新面板中,然后添加另一个带有该面板的选项卡。希望对您有所帮助。
    • 嘿伙计,我编辑了我的代码,我相信,你的建议,但是当我尝试编译它时,我收到了这个错误消息。 Landscape.Landscape.(Landscape.java:33) at landscape.Landscape.main(Landscape.java:189) 的线程“main”java.lang.NullPointerException 中的异常我将原始代码编辑为我正在工作的内容现在。出于某种原因,我觉得我杀了这个。
    • 你得到 NullPointerException 因为你没有实例化 mainFrame。您仍然必须拥有mainFrame=new JFrame("whatever title");。只需将其放回构造函数的开头,它就可以工作了。此外,在向面板添加任何内容之前,应放置“创建组件”部分;否则你会得到更多的 NullPointerExceptions。
    • 啊!傻我。我在粘贴我的代码时搞砸了顺序。如果没有定义它就不能向面板添加任何东西。非常感谢您的帮助,我应该能够轻而易举地解决这个问题。
    【解决方案2】:

    tutorial 及其demo 是非常直接的示例。

    【讨论】:

    • 我一直在使用它并剪切我的代码并将其放入其中,但它无法正常工作。我的问题是公共类 () 和所有变量,它们去了哪里。我不相信我可以有超过 1 个公共课程,所以这让我感到困惑。
    • 每个类文件不能有多个公共类。您可以拥有任意数量的类文件*。在类文件中,您可以拥有任意数量的非公共类*。 (* 除非你的老师另有说明)。
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2012-08-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多