2013-12-08 32 views
0

如何設置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 */

+1

什麼是JText?我不知道這個名字的任何Swing組件。 – camickr

回答

2

基本上,您需要使用JFrame和頂部JPanel上的GridBagLayout來獲取所需的間距。

您還需要在Event Dispatch線程(EDT)上啓動Swing應用程序。

這裏的GUI:

Calculator

下面是修改後的代碼:

/* Calculator */ 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Label; 
import java.awt.Panel; 
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.JLabel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

class Calculator extends JFrame implements ActionListener { 
    private static final long serialVersionUID = 4472395095500512410L; 
    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(); 
    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(25); 
     txt2 = new JTextField(25); 
     txt3 = new JTextField(25); 
     txt1.setFont(font1); 
     txt2.setFont(font1); 
     txt3.setFont(font1); 

     Label1.setFont(font1); 
     Label2.setFont(font1); 
     Label3.setFont(font1); 

     p1.setLayout(new GridBagLayout()); 
     GridBagConstraints pc = new GridBagConstraints(); 

     // Adding lables and textbox to panel p1 
     pc.gridx = 0; 
     pc.gridy = 0; 
     pc.anchor = GridBagConstraints.LINE_START; 
     pc.fill = GridBagConstraints.NONE; 

     p1.add(Label1, pc); 

     pc.gridx++; 
     pc.fill = GridBagConstraints.HORIZONTAL; 

     p1.add(txt1, pc); 

     pc.gridx = 0; 
     pc.gridy++; 
     pc.fill = GridBagConstraints.NONE; 

     p1.add(Label2, pc); 

     pc.gridx++; 
     pc.fill = GridBagConstraints.HORIZONTAL; 

     p1.add(txt2, pc); 

     pc.gridx = 0; 
     pc.gridy++; 
     pc.fill = GridBagConstraints.NONE; 

     p1.add(Label3, pc); 

     pc.gridx++; 
     pc.fill = GridBagConstraints.HORIZONTAL; 

     p1.add(txt3, pc); 
     // 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 GridBagLayout()); 
     // Adding 2 panels to this frame 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.anchor = GridBagConstraints.LINE_START; 
     c.fill = GridBagConstraints.HORIZONTAL; 
     this.add(p1, c); 
     c.gridy++; 
     this.add(p2, c); 

     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[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Calculator f = new Calculator(); 
//    f.setSize(500, 500); 
       f.pack(); 
       f.setVisible(true); 
      }   
     }); 

    } 
} 
+0

請幫我澄清屬性pc.gridx = 0;和pc.gridy = 0; GridBagConstraints pc。 – user3077408

+0

@ user3077408:GridBagLayout的網格以gridx = 0和gridy = 0開始。遞增gridx沿着水平軸。遞增網格是沿着垂直軸。 –