GUI 中的 Java 选项卡

发布于 2024-09-13 10:03:09 字数 6239 浏览 6 评论 0原文

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

  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();
    }
}

I am hoping someone can spoon feed me this solution. It is part of my major lab for the class, and it really isn't giving me too much since I just don understand how to make a GUI with tabs. I can make a regular program with some sort of GUI, but I've been searching and reading and can't put 2 and 2 because of the whole class part and declaring the private variables. So what I am asking is if someone can make me a main GUI with 5 tabs and put my code into 1 tab so I can learn and put the rest of my codes into the other 4 tabs. So I hope you don't think I want you to give me code when I have the code, I just don't really get the tabs, and we haven't gone over it in class. Here is the code with its own gui, I hope what I type makes sense. I learn code by seeing, and this will help me a bunch.

  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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-09-20 10:03:09

无需获取内容窗格并向其中添加内容,只需创建一个新的 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

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

Instead of getting the content pane and adding to it, just create a new JPanel and add your stuff to that. Then, add the panel to a new JTabbedPane with whatever title you want, and set the content pane of the JFrame to be the tabbed pane.

Here is a simple example of what you would do:

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

You can leave the rest of your code how it is and it should work fine.

源来凯始玺欢你 2024-09-20 10:03:09

教程及其demo 是非常简单的示例。

The tutorial and its demo are pretty straight forward examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文