如何設置panel1中JText的寬度與panel2中的寬度相等?如何設置panel1中JText的寬度與panel2中的寬度相等
/* Calculator */
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import javax.swing.*;
class Calculator extends Frame implements ActionListener {
Font font1 = new Font("Times New Roman", Font.PLAIN, 20);
JLabel Label1, Label2, Label3;
JTextField txt1, txt2, txt3;
// 4 JButton
JButton Add = new JButton(" + ");
JButton Sub = new JButton(" - ");
JButton Mul = new JButton(" x ");
JButton Div = new JButton("/");
JButton Power = new JButton("^");
JButton Sin = new JButton(" Sin ");
JButton Cos = new JButton(" Cos ");
JButton Tan = new JButton(" Tan ");
JButton Arcsin = new JButton(" Arcsin ");
JButton Arccos = new JButton(" Arccos ");
JButton Arctan = new JButton(" Arctan ");
JButton Factorial = new JButton(" Factorial ");
JButton Combinatory = new JButton(" Combinatory ");
// 2 Panels will contain components
Panel p1 = new Panel(new GridLayout(3, 2));
Panel p2 = new Panel(new GridLayout(4, 4));
/* Calculator() */
Calculator() {
super ("Calculator");
Label1 = new JLabel ("First number: ", Label.LEFT);
Label2 = new JLabel ("Second number: ", Label.LEFT);
Label3 = new JLabel ("Result: ", Label.LEFT);
txt1 = new JTextField(); txt2 = new JTextField(); txt3 = new JTextField();
txt1.setFont(font1); txt2.setFont(font1); txt3.setFont(font1);
txt1.setPreferredSize(new Dimension (100, 20));
txt2.setPreferredSize(new Dimension (100, 20));
txt3.setPreferredSize(new Dimension (100, 20));
Label1.setFont(font1); Label2.setFont(font1);
Label3.setFont(font1);
// Adding lables and textbox to panel p1
p1.setMaximumSize(new Dimension(3*100, 2*200));
p2.setMaximumSize(new Dimension(4*100, 4*100));
p1.add(Label1); p1.add(txt1);
p1.add(Label2); p1.add(txt2);
p1.add(Label3); p1.add(txt3);
// Adding 4 JButtons to panel p2
p2.add(Add); p2.add(Sub); p2.add(Mul); p2.add(Div);
p2.add(Power); p2.add(Factorial); p2.add(Combinatory);
p2.add(Sin); p2.add(Cos); p2.add(Tan);
p2.add(Arcsin); p2.add(Arccos); p2.add(Arctan);
// set layout of this frame is FlowLayout
this.setLayout(new FlowLayout());
// Adding 2 panels to this frame
this.add(p1); this.add(p2);
Add.addActionListener(this); Sub.addActionListener(this);
Mul.addActionListener(this); Div.addActionListener(this);
Power.addActionListener(this); Factorial.addActionListener(this);
Sin.addActionListener(this); Cos.addActionListener(this);
Tan.addActionListener(this); Arcsin.addActionListener(this);
Arccos.addActionListener(this); Arctan.addActionListener(this);
Combinatory.addActionListener(this);
// Managing window closing event
addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent event) { System.exit(0); }
});
/* Add close JButton or we can use:
setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); in constructor Calculator() */
}
public long Factorial (long n) {
int i, n1; n1 = 1;
for (i = 1; i <= n; i++)
n1 = n1*i;
return n1;
}
/* public void actionPerformed (ActionEvent e) */
public void actionPerformed (ActionEvent e) {
/* Method will be automatic called when ActionListener receive action
from the listened objects */
double k3; double PI = 3.141592654;
// Convert inputted content into number data
double k1 = Double.parseDouble (txt1.getText());
double k2 = Double.parseDouble (txt2.getText());
String s1, s2, s3, s4; s1 = txt1.getText(); s2 = txt2.getText();
if (e.getSource() == Add) {
// If event source is JButton Add
k3 = k1 + k2; s3 = Double.toString(k3);
s4 = s1 + " + " + s2 + " = " + s3;
txt3.setText(s4 );
/* txt3, s4 = Result */
}
if (e.getSource() == Sub) {
k3 = k1 - k2; s3 = Double.toString(k3);
s4 = s1 + " - " + s2 + " = " + s3;
txt3.setText(s4 );
}
if (e.getSource( ) == Mul) {
k3 = k1 * k2; s3 = Double.toString(k3);
s4 = s1 + " * " + s2 + " = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Div) {
k3 = k1/k2; s3 = Double.toString(k3);
s4 = s1 + "/" + s2 + " = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Power) {
k3 = Math.exp(Math.log(k1) * k2); s3 = Double.toString(k3);
s4 = s1 + "^" + s2 + " = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Factorial) {
long n, n1; n = (long) k1;
n1 = Factorial(n);
s3 = Long.toString(n1); s1 = Long.toString(n);
s4 = s1 + "! = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Sin) {
k1 = k1*PI/180;
k3 = Math.sin(k1); s3 = Double.toString(k3);
s4 = "Sin(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Cos) {
k1 = k1*PI/180;
k3 = Math.cos(k1); s3 = Double.toString(k3);
s4 = "Cos(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Tan) {
k1 = k1*PI/180;
k3 = Math.tan(k1); s3 = Double.toString(k3);
s4 = "Tan(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Arcsin) {
k3 = Math.asin(k1); k3 = k3*180/PI;
s3 = Double.toString(k3);
s4 = "Arcsin(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Arccos) {
k3 = Math.acos(k1); k3 = k3*180/PI;
s3 = Double.toString(k3);
s4 = "Arccos(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Arctan) {
k3 = Math.atan(k1); k3 = k3*180/PI;
s3 = Double.toString(k3);
s4 = "Arctan(" + s1 + ") = " + s3;
txt3.setText(s4 );
}
if (e.getSource() == Combinatory) {
long n1, n2, n; n1 = (long) k1; n2 = (long) k2;
n = Factorial(n2)/(Factorial(n1) * Factorial(n2 - n1) );
s1 = Long.toString(n1); s2 = Long.toString(n2);
s3 = Long.toString(n);
s4 = "C(" + s1 + ", " + s2 + ") = " + s3;
txt3.setText(s4 );
}
}
public static void main(String args[]) {
Calculator f = new Calculator();
f.setSize(500, 500);
f.setVisible(true);
}
}
請問您能告訴我如何設置panel1中JText的寬度等於panel2中的寬度? 我已經設置了
p1.setMaximumSize(new Dimension(3*100, 2*200));
p2.setMaximumSize(new Dimension(4*100, 4*100));
但它不起作用。
/*隨機文本,它看起來像你的代碼主要是代碼,我認爲我給英語足夠的細節。我需要這一行來發布代碼並稍後編輯:D */
什麼是JText?我不知道這個名字的任何Swing組件。 – camickr