简体   繁体   English

Java选项卡式窗格未显示程序

[英]Java tabbed pane not showing programs

I'm fairly inexperienced with Java and have searched around quite a bit to resolve this issue. 我对Java缺乏经验,并且进行了很多搜索来解决此问题。 I believe I have the code correct but my tabs are not showing anything. 我相信我的代码正确,但是我的标签没有显示任何内容。 I came across something about changing the layout to BorderLayout but it didn't work for me. 我遇到了一些有关将布局更改为BorderLayout的事情,但对我而言不起作用。 When I run the program I can see a very quick glimpse of my first tab program then it's blank. 当我运行该程序时,我可以快速浏览一下我的第一个选项卡程序,然后空白。 I posted just the TabbedPrograms class because I believe the problem lies in here but I can post the rest if needed. 我只发布了TabbedPrograms类,因为我认为问题出在这里,但如有需要,我可以发布其余内容。 Thank you in advance for any help. 预先感谢您的任何帮助。

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;


public class TabbedPrograms extends JFrame
{
public TabbedPrograms()
{
    setTitle("Week Four Lab Assignment");
    setLayout(new BorderLayout()); //this is what I added to change from the default layout
    JTabbedPane jtp = new JTabbedPane();
    getContentPane().add(jtp);

    jtp.addTab("Day GUI", new DayGUI());
    jtp.addTab("Office Area Calculator", new OfficeAreaCalculator());

    getContentPane().add(jtp);
    setSize(310, 210);
    setVisible(true);
}

public static void main(String[] args) 
{
    TabbedPrograms test = new TabbedPrograms();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DayGUI extends JPanel 
{
private JFrame mainFrame;
private JButton cmdGood;
private JButton cmdBad;

public DayGUI()
{
    mainFrame = new JFrame("Messages");

    cmdGood = new JButton("Good");
    cmdBad = new JButton("Bad");

    Container c = mainFrame.getContentPane();
    c.setLayout(new FlowLayout());
    c.setBackground(Color.orange);

    c.add(cmdGood);
    c.add(cmdBad);

    cmdGood.setMnemonic('G');
    cmdBad.setMnemonic('B');

    mainFrame.setSize(300, 100);

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

});

    ButtonsHandler bhandler = new ButtonsHandler();
    cmdGood.addActionListener(bhandler);
    cmdBad.addActionListener(bhandler);

    mainFrame.setVisible(true);
}

class ButtonsHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == cmdGood)
            JOptionPane.showMessageDialog(null, "Today is a good day!",
                    "Event Handler Message",
                    JOptionPane.INFORMATION_MESSAGE);

        if(e.getSource() == cmdBad)
            JOptionPane.showMessageDialog(null, "Today is a bad day!",
                    "Event Handler Message",
                    JOptionPane.INFORMATION_MESSAGE);
    }
}

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

}

}


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OfficeAreaCalculator extends JPanel
{
private JFrame mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField areaField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel areaLabel;

public OfficeAreaCalculator()
{
    mainFrame = new JFrame("Office Area Calculator");

    exitButton = new JButton("Exit");
    lengthLabel = new JLabel("Enter the length of the office:");
    widthLabel = new JLabel("Enter the width of the office:");
    areaLabel = new JLabel("Office area:");
    lengthField = new JTextField(5);
    widthField = new JTextField(5);
    areaField = new JTextField(5);
    areaField.setEditable(false);
    calculateButton = new JButton("Calculate");

    Container c = mainFrame.getContentPane();

    c.setLayout(new FlowLayout());

    c.add(lengthLabel);
    c.add(lengthField);
    c.add(widthLabel);
    c.add(widthField);
    c.add(areaLabel);
    c.add(areaField);
    c.add(calculateButton);
    c.add(exitButton);

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

    mainFrame.setSize(260, 150);

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


    CalculateButtonHandler chandler = new CalculateButtonHandler();
    calculateButton.addActionListener(chandler);

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

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

    mainFrame.setVisible(true);
        }

class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        DecimalFormat num = new DecimalFormat(", ###.##");
        double width, length, area;
        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);

        area = length * width;
        areaField.setText(num.format(area));

    }
}

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)
        {
            areaField.setText("");
        }
        else if (e.getSource() == areaField)
        {
            calculateButton.requestFocus();
        }
    }

    public void focusLost(FocusEvent e)
    {
        if(e.getSource() == widthField)
        {
            calculateButton.requestFocus();
        }
    }
}

}

The problem is that both DayGUI and OfficeAreaCalculator both add components to to their own individual JFrame windows rather than to TabbedPrograms . 问题在于, DayGUIOfficeAreaCalculator都将组件添加到其各自的JFrame窗口中,而不是添加到TabbedPrograms Those frames appear simultaneously then the main application frame is displayed. 这些框架同时出现,然后显示主应用程序框架。

In both those containers add the components directly onto the JPanel so that the components appear on the JTabbedPane 在这两个容器中,将组件直接添加到JPanel上,以便组件出现在JTabbedPane

For example 例如

//c.add(lengthLabel);
add(lengthLabel);

Something is wrong with your code logic, you wish to add a Day() and Calc() objects to your JTabbedPane, both of which extend JPanel, which you should use and add elements to when you add them to your tabbed pane. 您的代码逻辑出了点问题,您希望将Day()和Calc()对象添加到JTabbedPane中,这两个对象都扩展了JPanel,在将它们添加到选项卡式窗格时,应该使用它们并向其中添加元素。 Instead in your Day() and Calc() objects you instantiate new JFrame() objects which I don't seem to see (getters) inside your main function. 相反,在Day()和Calc()对象中,您实例化了新的JFrame()对象,但我似乎在主函数中看不到(获取器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM