java编写程序,单击按钮,改变框架窗口的大小 5
4个回答
展开全部
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
/**
* MainForm.java Created by karin at 2012-9-11
*/
public class MainForm extends JFrame implements ActionListener {
public static void main(String[] args) { // TODO Auto-generated method stub
int count = 0;
MainForm mainFrame = new MainForm();
mainFrame.setTitle("主窗体");
mainFrame.setBounds(new Rectangle(200, 200, 500, 500));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = mainFrame.getContentPane();
c.setLayout(new BorderLayout()); // 进行顺序排放组件的设置
c.setLayout(null);
JButton btn = new JButton("更改大小");
btn.addActionListener(mainFrame);
btn.setBounds(50, 50, 100, 100);
btn.setVisible(true);
c.add(btn);
mainFrame.setVisible(true); }
public void actionPerformed(ActionEvent e) {
this.setBounds(new Rectangle(200, 200, 300, 300));
}
}
import javax.swing.*;
/**
* MainForm.java Created by karin at 2012-9-11
*/
public class MainForm extends JFrame implements ActionListener {
public static void main(String[] args) { // TODO Auto-generated method stub
int count = 0;
MainForm mainFrame = new MainForm();
mainFrame.setTitle("主窗体");
mainFrame.setBounds(new Rectangle(200, 200, 500, 500));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = mainFrame.getContentPane();
c.setLayout(new BorderLayout()); // 进行顺序排放组件的设置
c.setLayout(null);
JButton btn = new JButton("更改大小");
btn.addActionListener(mainFrame);
btn.setBounds(50, 50, 100, 100);
btn.setVisible(true);
c.add(btn);
mainFrame.setVisible(true); }
public void actionPerformed(ActionEvent e) {
this.setBounds(new Rectangle(200, 200, 300, 300));
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
添加一个 resize()试试看。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
class Jframe extends JFrame {
public Jframe() {
add();
button();
}
public void add() {
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setVisible(true);
}
public void button() {
JButton j = new JButton("点击");
j.setBounds(99, 66, 60, 30);
j.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Jframe.this.setSize(500, 400);
Jframe.this.repaint();
}
});
this.add(j);
}
public static void main(String[] args) {
new Jframe();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
class Jframe extends JFrame {
public Jframe() {
add();
button();
}
public void add() {
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setVisible(true);
}
public void button() {
JButton j = new JButton("点击");
j.setBounds(99, 66, 60, 30);
j.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Jframe.this.setSize(500, 400);
Jframe.this.repaint();
}
});
this.add(j);
}
public static void main(String[] args) {
new Jframe();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JFrame.setSize 方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询