我完成了作业,我只有一个小问题。 “标题”行显示在与程序其余部分相同的行中。这是它的外观:
我需要“整数计算”在该行中,居中,以及其下的所有其他内容。理性的一样。我明白这是因为FlowLayout,但有没有办法在这上面使用新的一行?我在某处可以看到JTextArea可以使用,但我不知道如何实现它,特别是我目前所有的文本字段。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HW3TabbedPane extends JFrame {
private JPanel intPanel = new JPanel();
private JPanel intPanel1 = new JPanel();
private JPanel intPanel2 = new JPanel();
private JPanel ratPanel = new JPanel();
private JPanel ratPanel1 = new JPanel();
private JPanel ratPanel2 = new JPanel();
private JTabbedPane tab = new JTabbedPane();
private FlowLayout flow = new FlowLayout();
private BorderLayout intBorder = new BorderLayout();
private BorderLayout ratBorder = new BorderLayout();
private JButton intAdd = new JButton("Add");
private JButton intSubstract = new JButton("Substract");
private JButton intMultiply = new JButton("Multiply");
private JButton intDivide = new JButton("Divide");
private JButton ratAdd = new JButton("Add");
private JButton ratSubstract = new JButton("Substract");
private JButton ratMultiply = new JButton("Multiply");
private JButton ratDivide = new JButton("Divide");
private JLabel intLabel = new JLabel("Integer Calculation");
private JLabel ratLabel = new JLabel ("Rational Calculation");
private JLabel n1IntLabel = new JLabel("Number 1");
private JLabel n2IntLabel = new JLabel("Number 2");
private JLabel n1RatLabel = new JLabel("Number 1");
private JLabel n2RatLabel = new JLabel("Number 2");
private JLabel intResultLabel = new JLabel("Result");
private JLabel ratResultLabel = new JLabel("Result");
private JTextField n1IntField = new JTextField();
private JTextField n2IntField = new JTextField();
private JTextField intResultField = new JTextField();
private JTextField n1RatField = new JTextField();
private JTextField n2RatField = new JTextField();
private JTextField ratResultField = new JTextField();
private Integer x;
private Integer y;
private Double w;
private Double z;
public HW3TabbedPane() {
intPanel1.setLayout(flow);
intPanel1.add(intLabel);
intPanel1.add(n1IntLabel);
intPanel1.add(n1IntField);
intPanel1.add(n2IntLabel);
intPanel1.add(n2IntField);
intPanel1.add(intResultLabel);
intPanel1.add(intResultField);
intPanel2.add(intAdd);
intPanel2.add(intSubstract);
intPanel2.add(intMultiply);
intPanel2.add(intDivide);
ratPanel1.setLayout(flow);
ratPanel1.add(ratLabel);
ratPanel1.add(n1RatLabel);
ratPanel1.add(n1RatField);
ratPanel1.add(n2RatLabel);
ratPanel1.add(n2RatField);
ratPanel1.add(ratResultLabel);
ratPanel1.add(ratResultField);
ratPanel2.add(ratAdd);
ratPanel2.add(ratSubstract);
ratPanel2.add(ratMultiply);
ratPanel2.add(ratDivide);
n1IntField.setColumns(3);
n2IntField.setColumns(3);
intResultField.setColumns(4);
intResultField.setEditable(false);
n1RatField.setColumns(3);
n2RatField.setColumns(3);
ratResultField.setColumns(4);
ratResultField.setEditable(false);
intPanel.setLayout(intBorder);
intPanel.add(intPanel1, BorderLayout.NORTH);
intPanel.add(intPanel2, BorderLayout.SOUTH);
ratPanel.setLayout(ratBorder);
ratPanel.add(ratPanel1, BorderLayout.NORTH);
ratPanel.add(ratPanel2, BorderLayout.SOUTH);
JTabbedPane tab = new JTabbedPane();
tab.addTab("Integer Operations", intPanel);
tab.addTab("Rational Operations", ratPanel);
add(tab);
intAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int res;
x = Integer.parseInt(n1IntField.getText());
y = Integer.parseInt(n2IntField.getText());
res = x+y;
intResultField.setText(""+res);
}
});
intSubstract.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int res;
x = Integer.parseInt(n1IntField.getText());
y = Integer.parseInt(n2IntField.getText());
res = x-y;
intResultField.setText(""+res);
}
});
intMultiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int res;
x = Integer.parseInt(n1IntField.getText());
y = Integer.parseInt(n2IntField.getText());
res = x*y;
intResultField.setText(""+res);
}
});
intDivide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int res;
x = Integer.parseInt(n1IntField.getText());
y = Integer.parseInt(n2IntField.getText());
res = x/y;
intResultField.setText(""+res);
}
});
ratAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double res;
w = Double.parseDouble(n1RatField.getText());
z = Double.parseDouble(n2RatField.getText());
res = w+z;
ratResultField.setText(""+res);
}
});
ratSubstract.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double res;
w = Double.parseDouble(n1RatField.getText());
z = Double.parseDouble(n2RatField.getText());
res = w-z;
ratResultField.setText(""+res);
}
});
ratMultiply.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double res;
w = Double.parseDouble(n1RatField.getText());
z = Double.parseDouble(n2RatField.getText());
res = w*z;
ratResultField.setText(""+res);
}
});
ratDivide.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double res;
w = Double.parseDouble(n1RatField.getText());
z = Double.parseDouble(n2RatField.getText());
res = w/z;
ratResultField.setText(""+res);
}
});
}
public static void main(String[] args) {
HW3TabbedPane frame = new HW3TabbedPane();
frame.setTitle("Tabbed Pane");
frame.setSize(400, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
答案 0 :(得分:5)
请注意,这与JLabel有一个新行无关,而与智能使用布局管理器放置组件有关。这里的秘诀是简单地使用更多JPanel,每个JPanel都有自己的布局,从而组合布局。例如,
// Center the text in the JLabel using SwingConstants
private JLabel intLabel = new JLabel("Integer Calculation", SwingConstants.CENTER);
// ...
public HW3TabbedPane() {
// GridLayout to hold JPanels in one column and in several rows
intPanel1.setLayout(new GridLayout(0, 1));
intPanel1.add(intLabel); // add the title JLabel to the top
// next row's JPanel
JPanel nextJPanel = new JPanel(); // leave it FlowLayout
nextJPanel.add(n1IntLabel);
nextJPanel.add(n1IntField);
nextJPanel.add(n2IntLabel);
nextJPanel.add(n2IntField);
nextJPanel.add(intResultLabel);
nextJPanel.add(intResultField);
// add to the intPanel1
intPanel1.add(nextJPanel);