java swing 单击窗口或者按钮出现一个矩形
作业是给一个conwaysgameoflife加上GUI一直以为很简单。。但是快到期末才发现SWING好难完全搞不懂、、用swing的UI创建WINDOW或者直接写代码完...
作业是给一个conways game of life加上GUI 一直以为很简单。。但是快到期末才发现SWING好难 完全搞不懂、、用swing的UI创建WINDOW或者直接写代码完全不运行。。自己也完全不知道该干什么了。。。求助。。只要把画矩形搞明白后面就可以自己摸索了 拜托了! 下面的是看了一些帖子后自己弄出来的。。用SWING design创建的程序一直提醒空指针。。如果可以的话求+qq解答。。。752824100
package exer1;
import java.awt.Graphics;
public class Main {
public static void main(String[] args) {
Initialise ini = new Initialise();
ini.frame.setBounds(0, 0, 800, 600);
ini.create();
ini.panel.repaint();
ini.frame.setVisible(true);
}
}
package exer1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Initialise implements MouseListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Graphics e=null;
public void create() {
panel.setBounds(0, 0, 800,600);
frame.add(panel);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 40, 40);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
panel.repaint();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
} 展开
package exer1;
import java.awt.Graphics;
public class Main {
public static void main(String[] args) {
Initialise ini = new Initialise();
ini.frame.setBounds(0, 0, 800, 600);
ini.create();
ini.panel.repaint();
ini.frame.setVisible(true);
}
}
package exer1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Initialise implements MouseListener {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Graphics e=null;
public void create() {
panel.setBounds(0, 0, 800,600);
frame.add(panel);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 40, 40);
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
panel.repaint();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
} 展开
2个回答
展开全部
这个是学习看看吧!!
public class test9 extends ApplicationFrame {
public test9(String title) {
super(title);
key = (int)(Math.random() * 100);
initComponents();
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
int start=0;
int end=100;
int key=50;
public static void main(String[]args){
new test9("猜数字");
}
private void initComponents() {
// TODO 自动产生的方法 Stub
jPanel1 = new javax.swing.JPanel();
jPanel1.setPreferredSize(new Dimension(250,150));
add(jPanel1);
jPanel1.setLayout(null);
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1.setText("猜数字游戏:0~100");
jLabel1.setHorizontalAlignment(javax.swing.JTextField.LEFT);
jLabel1.setSize(120, 20);
jLabel1.setLocation(10, 10);
jPanel1.add(jLabel1);
jTextField1.setFont(new java.awt.Font("新细明体", 1, 12));
jTextField1.setSize(100, 20);
jTextField1.setLocation(20, 40);
jTextField1.enableInputMethods(false);
jPanel1.add(jTextField1);
jButton1.setText("确定");
jButton1.setFont(new java.awt.Font("新细明体", 1, 14));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonActionPerformed(evt);
}
private void jButtonActionPerformed(ActionEvent evt) {
try{
Numberguess(Integer.parseInt(jTextField1.getText()));
jTextField1.setText("");
} catch(Exception e) {
new Thread(new Runnable() {//跳出提示框
public void run() {
JOptionPane.showMessageDialog(null, "请输入整数。");
}
}).start();
}
}
});
jButton1.setSize(100, 20);
jButton1.setLocation(10, 80);
jPanel1.add(jButton1);
jButton2.setText("重新开始");
jButton2.setFont(new java.awt.Font("新细明体", 1, 14));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonActionPerformed(evt);
}
private void jButtonActionPerformed(ActionEvent evt) {
jTextField1.setText("");
jLabel1.setText("猜数字游戏:0~100");
key = (int)(Math.random() * 100);
start=0;
end=100;
}
});
jButton2.setSize(100, 20);
jButton2.setLocation(120, 80);
jPanel1.add(jButton2);
}
public void Numberguess(int i){
if(i==key){
jLabel1.setText("恭喜你答对了!");
}else if(i<key && start<i){
start=i;
jLabel1.setText("猜数字游戏:"+start+"~"+end);
}else if(i>key && i<end){
end=i;
jLabel1.setText("猜数字游戏:"+start+"~"+end);
}else{
new Thread(new Runnable() {//跳出提示框
public void run() {
JOptionPane.showMessageDialog(null, "输入范围超过。");
}
}).start();
}
}
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTextField jTextField1;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询