我正在创建一个计算某些形状区域的程序。当我运行程序时,选择一个形状并输入该区域的值,它会为所有形状正确计算区域。但是,如果我想在不关闭程序的情况下计算另一个形状的区域,它只会在前一个形状之后添加框而不是切换它们。我想知道是否有办法让它只在你改变形状时显示一组文本字段?我的代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainPanel extends JPanel implements ActionListener {
JComboBox shapeList;
JTextField answer;
JTextField shapeSelected;
JTextField circle;
JTextField square;
JTextField triangle;
JTextField triangle3;
JLabel circle2;
JLabel square2;
JLabel triangle2;
JLabel triangle4;
JLabel triangle5;
JLabel squareAnswer;
JLabel circleAnswer;
JLabel triangleAnswer;
JButton compute;
MainPanel()
{
String [] shapes = {"circle", "square", "triangle"};
shapeList = new JComboBox(shapes);
shapeList.addActionListener(this);
add(shapeList);
compute = new JButton("Compute");
compute.addActionListener(this);
add(compute);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if (o == shapeList)
{
if (shapeList.getSelectedItem().equals("circle"))
{
circle2 = new JLabel();
circle2.setText("Radius:");
add(circle2);
circle = new JTextField(10);
add(circle);
this.revalidate();
this.repaint();
}
if (shapeList.getSelectedItem().equals("square"))
{
square2 = new JLabel();
square2.setText("Width/Height:");
add(square2);
square = new JTextField(10);
add(square);
this.revalidate();
this.repaint();
}
if (shapeList.getSelectedItem().equals("triangle"))
{
triangle4 = new JLabel();
triangle4.setText("Base:");
add(triangle4);
triangle = new JTextField(10);
add(triangle);
triangle5 = new JLabel();
triangle5.setText("Height:");
add(triangle5);
triangle3 = new JTextField(10);
add(triangle3);
this.revalidate();
this.repaint();
}
}
if (o == compute)
{
if (shapeList.getSelectedItem().equals("square"))
{
squareAnswer = new JLabel();
squareAnswer.setText("Area:");
add(squareAnswer);
answer = new JTextField(10);
add(answer);
this.revalidate();
this.repaint();
double sum = Double.parseDouble(square.getText()) * Double.parseDouble(square.getText());
answer.setText(String.valueOf(sum));
}
if (shapeList.getSelectedItem().equals("triangle"))
{
triangleAnswer = new JLabel();
triangleAnswer.setText("Area:");
add(triangleAnswer);
answer = new JTextField(10);
add(answer);
this.revalidate();
this.repaint();
double sum = (Double.parseDouble(triangle.getText()) * Double.parseDouble(triangle3.getText())) / 2;
answer.setText(String.valueOf(sum));
}
if (shapeList.getSelectedItem().equals("circle"))
{
circleAnswer = new JLabel();
circleAnswer.setText("Area:");
add(circleAnswer);
answer = new JTextField(10);
add(answer);
this.revalidate();
this.repaint();
double sum = (Double.parseDouble(circle.getText()) * Double.parseDouble(circle.getText())) * Math.PI;
answer.setText(String.valueOf(sum));
}
}
}
}
答案 0 :(得分:2)
使用import * as React from 'react';
declare global {
interface Function {
style: any
}
}
describe('static inheritance', () => {
class StyleableComponent<P, S> extends React.Component<P, S> {
protected classes: any;
static style: any;
constructor(props?: P, context?: any, public style: any = "default") {
super(props, context);
}
someMethod() {
//dangerous if subclass not define static style property
//todo:the 1st approach
// let style = Object.getPrototypeOf(this).constructor.style;
// return style;
//todo:the 2nd approach
// let cotr: any = this.constructor;
// return cotr.style;
//todo:the 3nd approach,you must declare style in Function interface
return this.constructor.style;
}
}
class Foo extends StyleableComponent<any, any> {
static style = "foo";
constructor(props?: any, context?: any) {
super(props, context, Foo.style);
}
}
class Bar extends StyleableComponent<any, any> {
}
test('access style from subclass', function () {
let foo = new Foo();
expect(foo.someMethod()).toBe(Foo.style);
});
test('return undefined if subclass not define style', function () {
let bar = new Bar();
expect(bar.someMethod()).toBeUndefined();
});
test('replace static style with instance property', function () {
let foo = new Foo();
let bar = new Bar();
let baz = new Bar(null, null, "baz");
expect(foo.style).toBe("foo");
expect(bar.style).toBe("default");
expect(baz.style).toBe("baz");
});
});
。从组合框中选择形状时,可以交换指定形状的面板。
阅读How to Use CardLayout上Swing教程中的部分,了解更多信息和工作示例。
工作示例显示了在组合框中选择项目时如何摇动面板。