我需要一个 GUI,它应该有两行两列 [第一列带有标签,第二列带有文本字段]。我已经编写了下面的代码,但我无法将其放入列和行中。请您帮忙将其放入列和行中。
public void showstate(){
MyPanel b=new MyPanel();
f.setBounds(50, 50, 800, 600);
f.add(b);
b.updateState(result);
f.setVisible(true);
System.out.println("Number of times AC is on:" + AcCounter);
System.out.println("Number of times Heater is on:" + HeaterCounter);
}
class MyPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField t;
private JTextField v;
public MyPanel() {
//setBorder(BorderFactory.createLineBorder(Color.black));
JLabel l = new JLabel("THE STATUS OF AC IS");
this.add(l);
t = new JTextField("<html><table><tr><td></td><td></td></tr><br></br></table></html>");
this.add(t);
//JLabel l = new JLabel("");
l.setBounds(100, 100, 30, 30);
JLabel p = new JLabel("THE STATUS OF HEATER IS");
p.setAlignmentX(LEFT_ALIGNMENT);
p.setHorizontalAlignment(30);
p.setBounds(100, 200, 30, 30);
this.add(p);
v = new JTextField("<td></td></tr></table></html>");
this.add(v);
// JLabel m = new JLabel("<html> <span><br><br><br>Accounter</br></br></br></span> </html> ",JLabel.CENTER);
//m.setBounds(100, 300, 30, 30);
// JLabel n = new JLabel("<html> <span><br><br><br>Heatercounter</br></br></br></span></html> ", JLabel.CENTER);
//n.setBounds(100, 400, 30, 30);
t.setBounds(300, 400, 30, 50);
// q = new JTextField("");
// w = new JTextField("");
// this.add(m);
// this.add(q);
// this.add(n);
// this.add(w);
}
public void updateState(String result) {
String ac, ab;
// double AcIsOn,HeaterIsOn;
if ("aboveHt".equals(result) || "coolAir".equals(result)) {
ac = "cooler on";
AcCounter++;
} else {
ac = "cooler off";
}
if(temperature==72){
ab = "Heater off";
}
else if ("belowHt".equals(result)) {
ab = "Heater on";
HeaterCounter++;
// AcCounter=1;
}
else {
ab = "Heater off";
}
t.setText(ac);
v.setText(ab);
}
}
我在一行中得到输出为Ac JTextField 的状态 Heater JTextField的状态