作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Java 相当缺乏经验,并且已经进行了大量搜索来解决这个问题。我相信我的代码是正确的,但我的选项卡没有显示任何内容。我遇到了一些关于将布局更改为 BorderLayout 的事情,但它对我不起作用。当我运行该程序时,我可以很快看到我的第一个选项卡程序,然后它是空白的。我只发布了 TabbedPrograms 类,因为我相信问题就在这里,但如果需要的话我可以发布其余的内容。预先感谢您的帮助。
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();
}
}
}
}
最佳答案
问题在于 DayGUI
和 OfficeAreaCalculator
都将组件添加到各自的 JFrame
窗口中,而不是添加到 TabbedPrograms
。这些框架同时出现,然后显示主应用程序框架。
在这两个容器中,将组件直接添加到 JPanel
上,以便组件显示在 JTabbedPane
上
例如
//c.add(lengthLabel);
add(lengthLabel);
关于Java 选项卡式 Pane 不显示程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989343/
我是一名优秀的程序员,十分优秀!