当前位置: 代码迷 >> Java相关 >> GUI源程序代码分析!
  详细解决方案

GUI源程序代码分析!

热度:283   发布时间:2006-01-09 13:38:00.0
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  代码  

----------------解决方案--------------------------------------------------------
package 语句和 main 函数不能同时出现。
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
俺不是高手拉 ,但是你的代码加了你的自建类我运行不了,要直接从“这么”长的代码里发现错误,偶是不行滴
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
package digzag;
import digzag.*;
以上2句删掉就OK了
----------------解决方案--------------------------------------------------------

已经搞好了!
谢谢了!


----------------解决方案--------------------------------------------------------
  相关解决方案