我需要获得一个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 加热器的状态 JTextField 在一行中
答案 0 :(得分:3)
在Swing中布局内容的正确方法是使用布局管理器。您的面板具有FlowLayout的默认布局,它将所有组件放在一行中,就像您正在观察的那样。
您可以使用GridLayout或BoxLayout的组合或更复杂的东西。请the tutorials for Layout Managers做出选择。
答案 1 :(得分:1)
我建议您阅读LayoutManager.它可以帮助您构建GUI。
以下是GridLayout
的示例,1列有2个标签和2个第2列字段:
public class Example extends JFrame {
public Example() {
setLayout(new GridLayout(2,2));
JLabel l1 = new JLabel("l1");
JLabel l2 = new JLabel("l2");
JTextField f1 = new JTextField();
JTextField f2 = new JTextField();
add(l1);
add(f1);
add(l2);
add(f2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String...strings ){
Example e = new Example();
}
}
答案 2 :(得分:1)
您可以通过多种方式实现这一目标。
正如已经说过的那样,在使用Swing时你应该利用正确的布局管理器,Swing API是围绕它们设计的(或者考虑到它们)。
另一种方法是使用JTable
警告:这是一个非常简单的示例,并假定单行表。就个人而言,我更喜欢允许多行,但这可以得到基本的想法......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
public class TestTable {
public static void main(String[] args) {
new TestTable();
}
public TestTable() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MonitorTableModel model = new MonitorTableModel();
model.setAcStatus("Off");
model.setHeaterStatus("On");
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(scrollPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MonitorTableModel extends AbstractTableModel {
private String acStatus;
private String heaterStatus;
public void setAcStatus(String acStatus) {
this.acStatus = acStatus;
fireTableCellUpdated(1, 0);
}
public void setHeaterStatus(String heaterStatus) {
this.heaterStatus = heaterStatus;
fireTableCellUpdated(1, 1);
}
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public String getColumnName(int column) {
return column == 0 ? "AC Status" : "Heater Status";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return columnIndex == 0 ? acStatus : heaterStatus;
}
}
}
查看How to use tables了解详情