关于java的gui小程序
请各位高手们告诉我代码吧。用鼠标按下名为《圆》和《长方形》按钮时圆和长方形都会变大。按钮名字随便起都行请各位高手帮帮忙!...
请各位高手们告诉我代码吧。
用鼠标按下名为《圆》和《长方形》按钮时 圆和长方形都会变大。
按钮名字随便起都行
请各位高手帮帮忙! 展开
用鼠标按下名为《圆》和《长方形》按钮时 圆和长方形都会变大。
按钮名字随便起都行
请各位高手帮帮忙! 展开
1个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawTest extends JFrame {
JPanel buttonPanel=new JPanel();
JButton round=new JButton("圆");
JButton rectangle=new JButton("长方形");
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");
setLayout(null);
setSize(600,400);
setResizable(false);
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);
round.setBounds(170, 20, 60, 30);
rectangle.setBounds(350, 20, 80, 30);
buttonPanel.add(round);
buttonPanel.add(rectangle);
add(buttonPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(final Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillOval(roundX,roundY,roundW,roundH);
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);
repaint();
}
}
});
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
}
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawTest extends JFrame {
JPanel buttonPanel=new JPanel();
JButton round=new JButton("圆");
JButton rectangle=new JButton("长方形");
int roundX=130,roundY=150,roundW=50,roundH=50;
int rectangleX=400,rectangleY=150,rectangleW=50,rectangleH=50;
public void init(){
setTitle("圆和长方形");
setLayout(null);
setSize(600,400);
setResizable(false);
setLocationRelativeTo(null);
buttonPanel.setLayout(null);
buttonPanel.setBounds(0, 300, 600, 100);
round.setBounds(170, 20, 60, 30);
rectangle.setBounds(350, 20, 80, 30);
buttonPanel.add(round);
buttonPanel.add(rectangle);
add(buttonPanel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(final Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillOval(roundX,roundY,roundW,roundH);
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
round.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
roundX-=1;
roundY-=1;
roundW+=2;
roundH+=2;
g.fillOval(roundX,roundY,roundW,roundH);
repaint();
}
}
});
rectangle.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
rectangleX-=1;
rectangleY-=1;
rectangleW+=2;
rectangleH+=2;
g.fillRect(rectangleX,rectangleY,rectangleW,rectangleH);
repaint();
}
}
});
}
public static void main(String[] args) {
new DrawTest().init();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询