寫了一個很簡單的案例,可以參考和修改
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、巨鹿網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、HTML5、商城開發(fā)、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為巨鹿等各大城市提供網(wǎng)站開發(fā)制作服務。
import?java.awt.BorderLayout;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JDialog;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JTextField;
public?class?FromeDemo?extends?JFrame{
JButton?jbutton;
public?FromeDemo()?{
jbutton?=?new?JButton("彈出2個文本框");
jbutton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
JDialog?jd?=?new?JDialog();
jd.setBounds(320,?180,?260,?100);
jd.setTitle("彈出文本框");
jd.getContentPane().setLayout(new?GridLayout(2,?2));
jd.add(new?JLabel("文本框一"));
jd.add(new?JTextField(80));
jd.add(new?JLabel("文本框二"));
jd.add(new?JTextField(80));
jd.setModal(true);//確保彈出的窗口在其他窗口前面
jd.setVisible(true);
}
});
add(jbutton,BorderLayout.SOUTH);
setBounds(300,?100,?320,?320);
setTitle("測試");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public?static?void?main(String?args[])?{
new?FromeDemo();
}
}
兄弟幫你寫了一個:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("畫圓");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
加入在frame中的按鈕名為sure
Button sure=new Button("確定");
sure.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame1.setVisible(false);
Frame frame2=new Frame();
frame2.setVisible(true);
}
});
采用以下代碼即可:
JButton?btn=new?JButton(new?AbstractAction("關閉并打開")?{???
@Override
public?void?actionPerformed(ActionEvent?e)?{
oldFrame.dispose();//?關閉并銷毀,無需銷毀可采用oldFrame.setVisible(false);
newFrame.setVisible(true);//?打開新窗口
}
});