我有一个非常奇怪的问题.我正在为类似绘画的程序编写一个接口,并创建了一个扩展JLabel的类.我添加了基本组件和所需的变量,如下所示:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import shapes.*;
public class denemePanel extends JPanel {
int radius;
int height;
int width;
String color;
JButton drawButRect;
JButton drawButCirc;
JButton drawButRand;
JButton paintBut;
JLabel heightL;
JLabel widthL;
JLabel radiusL;
JLabel colorL;
JTextField colorF;
JTextField heightF;
JTextField widthF;
JTextField radiusF;
ShapeContainer shapes;
boolean drawRect;
boolean drawCirc;
boolean drawRand;
boolean isColor;
ButtonListener buttonListener;
public denemePanel( ShapeContainer shapes) {
height = -1;
radius = -1;
width = -1;
color = "";
drawRect = false;
drawCirc = false;
drawRand = false;
isColor = false;
buttonListener = new ButtonListener();
setLayout( new GridLayout( 0,7, 5, 5));
setPreferredSize( new Dimension( 700, 150));
shapes = this.shapes;
drawButRect = new JButton( "Draw!");
drawButCirc = new JButton( "Draw!");
drawButRand = new JButton( "Draw any!");
paintBut = new JButton( "Paint!");
drawButRect.addActionListener( buttonListener);
drawButCirc.addActionListener( buttonListener);
drawButRand.addActionListener( buttonListener);
paintBut.addActionListener( buttonListener);
heightL = new JLabel( "Height:");
widthL = new JLabel( "Width:");
radiusL = new JLabel( "Radius:");
colorL = new JLabel( "Color:");
heightF = new JTextField( "0");
colorF = new JTextField( "yellow/red");
widthF = new JTextField( "0");
radiusF = new JTextField( "0");
add( heightL);
add( heightF);
add( radiusL);
add( radiusF);
add( new JLabel( ""));
add( colorL);
add( colorF);
add( widthL);
add( widthF);
add( new JLabel( ""));
add( new JLabel( ""));
add( new JLabel( ""));
add( new JLabel( ""));
add( new JLabel( ""));
add( new JLabel( ""));
add( drawButRect);
add( new JLabel( ""));
add( drawButCirc);
add( drawButRand);
add( new JLabel( ""));
add( paintBut);
setVisible( true);
}
//Methods
public int getRadius() {
return radius;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public String getColor() {
return color;
}
//Methods end
//-------------------------------------ACTION LISTENER-----------------------------------------------
public class ButtonListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
if( e.getSource() == drawButRect) {
height = Integer.parseInt( heightF.getText());
width = Integer.parseInt( widthF.getText());
if( height > 0 && width > 0) {
drawRect = true;
drawCirc = false;
drawRand = false;
isColor = false;
}
else {
JOptionPane.showMessageDialog(null, "Not a valid input!");
drawRect = false;
drawCirc = false;
drawRand = false;
isColor = false;
}
}
else if( e.getSource() == drawButCirc) {
radius = Integer.parseInt( radiusF.getText());
if( radius > 0) {
drawRect = false;
drawCirc = true;
drawRand = false;
isColor = false;
}
else {
JOptionPane.showMessageDialog(null, "Not a valid input!");
drawRect = false;
drawCirc = false;
drawRand = false;
isColor = false;
}
}
else if( e.getSource() == drawButRand) {
drawRect = false;
drawCirc = false;
drawRand = true;
isColor = false;
}
else if( e.getSource() == paintBut) {
color = colorF.getText().toLowerCase();
if( color.equals( "red") || color.equals( "yellow")) {
drawRect = false;
drawCirc = false;
drawRand = false;
isColor = true;
}
else {
JOptionPane.showMessageDialog(null, "Not valid color -just red or yellow!");
drawRect = false;
drawCirc = false;
drawRand = false;
isColor = false;
}
}
}
}
//-----------------------------------------------------------------------------------------------------------
}
Run Code Online (Sandbox Code Playgroud)
在添加"方法"部分之前,我的代码是这样工作的:
在添加方法(只是方法)后,它变成了这样(没有组件可见,但就是它,它们实际上正在工作!所以,如果我按下我知道它在那里的按钮,它就像它应该的那样工作):
通过做一些评论和取消注释,我发现String方法与它无关(取消注释它不能解决问题,并且只是取消注释不会导致问题.).但整数返回方法会单独和一起导致此问题.
//Methods
public int getRadius() {
return radius;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public String getColor() {
return color;
}
//Methods end
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我将setVisible()添加到Panel但没有任何改变.谢谢大家!
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |