我有以下2个java类。我想做第三节课。第三个类应该是一个带有两个选项卡的选项卡式窗格。一个标签应该有我放置的第一个窗格,第二个标签应该有我放的第二个窗格。我无法弄清楚。在我破坏电脑之前请帮助我。我到处搜寻。我试过阅读oracle文档,它只是没有点击我,我猜。我一遍又一遍地阅读我的文字........
package Week4;
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.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.JTextField;
public class OfficeAreaCalculator extends JFrame{
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.setBackground(Color.white);
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();
}
}
}
public static void main(String arg[])
{
new OfficeAreaCalculator();
}
}
和
package Week4;
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 java.awt.*;
public class DayGUI extends JFrame
{
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.setBackground(Color.white);
c.setLayout(new FlowLayout());
c.add(cmdGood);
cmdGood.setBackground(Color.green);
c.add(cmdBad);
cmdBad.setBackground(Color.red);
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) {
// TODO Auto-generated method stub
DayGUI app;
app = new DayGUI();
}
}
答案 0 :(得分:1)
您无法将Jframe添加到选项卡式窗格。如果您的每个应用程序都是JPanel,您只需将它们添加到JTabbedPane。
每个JFrame类都使用JFrame,并且在框架中,每个类都创建其内容。 您可以重新排列代码,以便每个JFrame创建一个面板,就像Jframe一样创建其内容。然后将面板添加到JFrame中。你的前两个申请应该仍然有效。
对于您的第三个类,使用Jframe,将JTabbedPane添加到Jframe,然后将上面创建的两个面板(前两个面板中的一个)添加到选项卡式窗格。
最简单的方法可能是完成第3类只是改变前两个类来扩展JPanel而不是Jframe。 然后创建一个从JFrame扩展的类,并添加一个JTabbedPane,它添加两个面板。