Java 如何将panel1中JText的宽度设置为panel2中的宽度
如何将panel1中JText的宽度设置为panel2中的宽度Java 如何将panel1中JText的宽度设置为panel2中的宽度,java,swing,layout,user-interface,preferredsize,Java,Swing,Layout,User Interface,Preferredsize,如何将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.
/* 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 );
}
}
但它不起作用
/*随机文本,看起来你的代码大部分是代码,我想我用英语给出了足够的细节。我需要这一行来发布您的代码并稍后进行编辑:D*/基本上,您需要使用JFrame和顶部JPanel上的GridBagLayout,以获得您想要的间距 您还需要在事件分派线程(EDT)上启动Swing应用程序 以下是GUI: 以下是修改后的代码:
/* 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);
}
});
}
}
/*计算器*/
导入java.awt.Font;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入java.awt.Label;
导入java.awt.Panel;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
类计算器扩展JFrame实现ActionListener{
私有静态最终长serialVersionUID=4472395095500512410L;
Font font1=
新字体(“Times new Roman”,Font.PLAIN,20);
JLabel标签1、标签2、标签3;
JTextField txt1、txt2、txt3;
//4按钮
JButton Add=新JButton(“+”);
JButton Sub=新JButton(“-”);
JButton Mul=新JButton(“x”);
JButton Div=新JButton(“/”);
JButton Power=新JButton(“^”);
JButton Sin=新JButton(“Sin”);
JButton Cos=新JButton(“Cos”);
JButton Tan=新JButton(“Tan”);
JButton arcin=新JButton(“arcin”);
JButton Arccos=新JButton(“Arccos”);
JButton-Arctan=新JButton(“Arctan”);
JButton阶乘=新JButton(
“阶乘”);
JButton组合=新JButton(
“组合法”);
//2个面板将包含组件
面板p1=新面板();
面板p2=新面板(新网格布局(
4, 4));
/*计算器()*/
计算器(){
超级(“计算器”);
Label1=新的JLabel(“第一个数字:”,标签左);
Label2=新的JLabel(“第二个数字:”,标签左);
Label3=新的JLabel(“结果:”,Label.LEFT);
txt1=新的JTextField(25);
txt2=新的JTextField(25);
txt3=新的JTextField(25);
txt1.setFont(font1);
txt2.setFont(font1);
txt3.setFont(font1);
Label1.setFont(font1);
Label2.setFont(font1);
Label3.setFont(font1);
p1.setLayout(新的GridBagLayout());
GridBagConstraints pc=新的GridBagConstraints();
//将标签和文本框添加到面板p1
pc.gridx=0;
pc.gridy=0;
pc.anchor=gridbagstraints.LINE\u START;
pc.fill=GridBagConstraints.NONE;
p1.添加(标签1,pc);
pc.gridx++;
pc.fill=gridbag.HORIZONTAL;
p1.添加(txt1,pc);
pc.gridx=0;
pc.gridy++;
pc.fill=GridBagConstraints.NONE;
p1.添加(标签2,pc);
pc.gridx++;
pc.fill=gridbag.HORIZONTAL;
p1.添加(txt2,pc);
pc.gridx=0;
pc.gridy++;
pc.fill=GridBagConstraints.NONE;
p1.添加(标签3,pc);
pc.gridx++;
pc.fill=gridbag.HORIZONTAL;
p1.添加(txt3,pc);
//将4个按钮添加到面板p2
p2.添加(添加);
p2.加入(附属法例);
p2.添加(Mul);
p2.加入(Div);
p2.添加(电源);
p2.加(阶乘);
p2.加法(组合);
p2.加入(Sin);
p2.添加(Cos);
p2.添加(Tan);
p2.添加(Arcin);
p2.增加(阿科司);
p2.添加(Arctan);
//此框架的设置布局为FlowLayout
this.setLayout(新的GridBagLayout());
//将2个面板添加到此框架
GridBagConstraints c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=0;
c、 锚点=GridBagConstraints.LINE\u开始;
c、 填充=GridBagConstraints.HORIZONTAL;
添加(p1,c);
c、 gridy++;
添加(p2,c);
Add.addActionListener(这个);
Sub.addActionListener(本);
Mul.addActionListener(此);
Div.addActionListener(此);
Power.addActionListener(this);
Factorial.addActionListener(this);
Sin.addActionListener(这个);
Cos.addActionListener(本);
Tan.addActionListener(本);
arcin.addActionListener(此);
Arccos.addActionListener(本);
Arctan.addActionListener(这个);
addActionListener(this);
//管理窗口关闭事件
addWindowListener(新的WindowAdapter(){
公共作废窗口关闭(WindowEvent事件){
系统出口(0);
}
});
/*
*添加close JButton或我们可以使用:setDefaultCloseOperation(
*JFrame.DISPOSE_ON_CLOSE);在构造函数计算器()中
*/
}
公共长阶乘(长n){
intⅠ,n1;
n1=1;
对于(i=1;i什么是JText?我不知道有任何Swing组件的名称。请原谅
/* 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);
}
});
}
}