| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 849 人关注过本帖
标题:GUI源程序代码分析!
只看楼主 加入收藏
zjlily
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-9
收藏
 问题点数:0 回复次数:6 
GUI源程序代码分析!

这是一个GUI的程序,编译能够成功,但是运行时候,就出现错误了.
下面的黑体部分就是我编译和运行的结果.

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

D:\Documents and Settings\sunxia>D:\JBuilderX\jdk1.4\bin\javac d:\pen.java

D:\Documents and Settings\sunxia>D:\JBuilderX\jdk1.4\bin\java d:\pen
Exception in thread "main" java.lang.NoClassDefFoundError: d:\pen




下面就是我的程序:


package digzag;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.JComboBox;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import digzag.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JCheckBox;

public class pen extends JFrame {
// this is the value being set and displayed
int a=100;
int b=100;
int if_box=1;
int width=100;
int height=100;
boolean flag =true;
Color col = Color.black;

// components

private JPanel buttonPanel = new JPanel();
private DisplayPanel displayPanel = new DisplayPanel();
private JLabel panelTitle = new JLabel(" MY shape");
String[] s={"green","red","blue","yellow"};
JComboBox jComboBox1 = new JComboBox(s);
private ButtonGroup RG1 = new ButtonGroup();
private JRadioButton jRadioButton1 = new JRadioButton("square");
private JRadioButton jRadioButton2 = new JRadioButton("circle");
JSlider jSlider1 = new JSlider();
private JCheckBox checkbox1 =new JCheckBox("filled",true) ;
private JButton showButton = new JButton("RESET");

// listeners
private ComboBox1_actionAdapter jcbl = new ComboBox1_actionAdapter();
private pen.ShowButtonListener sbl = new ShowButtonListener();
private SliderListener slbl = new SliderListener();

// window constructor
public pen() {
super("my shape (u6) Window");
// components constructor
RG1.add(jRadioButton1);
RG1.add(jRadioButton2);
Container c = getContentPane();


// lay out window
c.setLayout(new BorderLayout());

// lay out button panel
buttonPanel.setLayout(new GridLayout(10, 1, 5, 5));

// add components to button panel
buttonPanel.add(panelTitle);
buttonPanel.add(jSlider1);
buttonPanel.add(jComboBox1);
buttonPanel.add(jRadioButton1);
buttonPanel.add(jRadioButton2);
buttonPanel.add(checkbox1);
buttonPanel.add(showButton);


// add components to window
c.add(buttonPanel, BorderLayout.CENTER);
c.add(displayPanel, BorderLayout.WEST);

// register components with listeners
// sbl is the ShowButtonListener
// jcbl is the PanelButtonListener
// slbl is the SliderListener
showButton.addActionListener(sbl);
jComboBox1.addItemListener(jcbl);
jRadioButton1.addItemListener(jcbl);
jRadioButton2.addItemListener(jcbl);
jSlider1.addChangeListener(slbl);
checkbox1.addItemListener(jcbl);
}

// listener class definitions

//ComboBox1_actionAdapter listener class definitions

private class ComboBox1_actionAdapter implements java.awt.event.ItemListener {
public void itemStateChanged(ItemEvent e) {
if (e.getItemSelectable() == jComboBox1) {
if (jComboBox1.getSelectedIndex() == 0) {
col = Color.GREEN;
displayPanel.repaint();
}
if (jComboBox1.getSelectedIndex() == 1) {
col = Color.red;
displayPanel.repaint();
}
if (jComboBox1.getSelectedIndex() == 2) {
col = Color.blue;
displayPanel.repaint();
}
if (jComboBox1.getSelectedIndex() == 3) {
col = Color.yellow;
displayPanel.repaint();
}

}
if (e.getItemSelectable()==jRadioButton1){
if_box=1;
displayPanel.repaint();

};
if (e.getItemSelectable()==jRadioButton2){
if_box=2;
displayPanel.repaint();
};
if (e.getItemSelectable()==checkbox1)
{
flag = ((JCheckBox)e.getSource()).isSelected();
displayPanel.repaint();

}

}

public void actionPerformed(ActionEvent e) {
}
}

// showbuttonListener listener class definitions
private class ShowButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent e) {
if_box=1;
width=100;
height=100;
col = Color.black;
displayPanel.repaint();

}
}


// SliderListener listener class definitions

class SliderListener implements ChangeListener {
JLabel tf;

public void stateChanged(ChangeEvent e) {
JSlider s1 = (JSlider)e.getSource();
width=s1.getValue();
height=s1.getValue();
displayPanel.repaint();

}
public SliderListener() {
}
}
// display panel class definition

public class DisplayPanel extends JPanel {
int h, w;
DisplayPanel() {
super();
// setPreferredSize(new Dimension(200,200));
setPreferredSize(new Dimension(300, 300));

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);
g.setColor(col);

if (flag){
if (if_box==1)
{g.fillRect(a, b, width, height);
}
if (if_box==2) {
g.fillOval(a,b,width,height);
}
}
if (flag==false){
if (if_box==1)
{g.drawRect(a, b, width, height);
};
if (if_box==2) {
g.drawOval(a,b,width,height);
};
}
}
}

public static void main(String[] args) {
pen frame = new pen();

frame.setSize(500, 400);

// frame.pack();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();

// put it into centre of the screen

int x = (screenSize.width - frameSize.width) / 2;
int y = (screenSize.height - frameSize.height) / 2;

frame.setLocation(x, y);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

搜索更多相关主题的帖子: GUI 代码 
2006-01-09 13:38
cll19820814
Rank: 2
等 级:新手上路
威 望:3
帖 子:328
专家分:0
注 册:2005-11-30
收藏
得分:0 
package 语句和 main 函数不能同时出现。

懵懵懂懂,看千遍而不会;设身处地,试一下就成功!
2006-01-09 13:58
zjlily
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-9
收藏
得分:0 

谢谢高手指教!
那我把那个package语句去掉了,还是运行不了

2006-01-09 14:12
cll19820814
Rank: 2
等 级:新手上路
威 望:3
帖 子:328
专家分:0
注 册:2005-11-30
收藏
得分:0 
俺不是高手拉 ,但是你的代码加了你的自建类我运行不了,要直接从“这么”长的代码里发现错误,偶是不行滴

懵懵懂懂,看千遍而不会;设身处地,试一下就成功!
2006-01-09 14:27
zjlily
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-9
收藏
得分:0 

程序是用Jbuild做的,在Jbuild环境下也能运行出结果,代码应该是没有问题的.但是就是在JDK下运行不了.
还望高手帮忙看看.

2006-01-09 15:04
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 
package digzag;
import digzag.*;
以上2句删掉就OK了

向着软件工程师的目标前进!
2006-01-09 16:58
zjlily
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-9
收藏
得分:0 

已经搞好了!
谢谢了!

2006-01-10 10:36
快速回复:GUI源程序代码分析!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015637 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved