ä½ è¯´çæ¯æå¼é模æ对è¯æ¡ã
å¦æ模æ对è¯æ¡ä¸å
³éï¼æ æ³æä½ä¸»çªä½ãå¦æé模æ对è¯æ¡ä¸å
³éï¼ä»ç¶å¯ä»¥æä½ä¸»çªä½ã
//æé 模æ对è¯æ¡
final Dialog d = new Dialog(this, "模æ对è¯æ¡", true);
//æé é模æ对è¯æ¡
final Dialog d = new Dialog(this, "模æ对è¯æ¡", false);
æ ·ä¾ç¨åºå¦ä¸ï¼
import java.awt.Dialog;
import java.awt.FlowLayout;
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;
public class Main extends JFrame implements ActionListener {
JButton btnModel, btnNonModel;
public Main() {
super("对è¯æ¡");
this.setLayout(new FlowLayout());
btnModel = new JButton("æå¼æ¨¡æ对è¯æ¡");
btnNonModel = new JButton("æå¼é模æ对è¯æ¡");
this.add(btnModel);
this.add(btnNonModel);
btnModel.addActionListener(this);
btnNonModel.addActionListener(this);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Main();
}
@Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = (JButton) arg0.getSource();
if(btn == btnModel) { //æå¼æ¨¡æ对è¯æ¡
final Dialog d = new Dialog(this, "模æ对è¯æ¡", true);
d.setSize(800, 600);
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
d.setVisible(false);
}
});
d.setVisible(true);
}
else if(btn == btnNonModel) { //æå¼é模æ对è¯æ¡
final Dialog d = new Dialog(this, "é模æ对è¯æ¡", false);
d.setSize(800, 600);
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
d.setVisible(false);
}
});
d.setVisible(true);
}
}
}
追é®é¦å
ï¼é常æè°¢ä½ çåçï¼ä½ åççæ¯å建ä¸ä¸ªåçªå£åï¼æè¿è¡äºä¸ï¼æ²¡é®é¢ãä¸è¿ï¼æé®é¢çéç¹æ¯ï¼è¦æä¹è®¾ç½®æè½è®©æå¼çåçªå£å§ç»å±
äºç¶çªå£çä¸é¨ï¼ä¹å°±æ¯è¯´ä¸ç®¡ç¶çªå£å¨ä»ä¹ä½ç½®æå¼ï¼ç¶åæå¼çåçªå£é½è¦å¨ç¶çªå£çä¸é¨ï¼ä¸æ¯åµå¨ç¶çªå£éï¼ååï¼ã
追çå¨
d.setSize(200, 200);
åæ·»å 代ç ï¼
d.setLocationRelativeTo(this);
使åçªä½åºç°å¨ç¶çªä½ä¸å¤®ã