所以我有两个班级:
public class TimeServer extends JPanel implements TimeVariables
{
public TimeServer()
{
JTextArea serverLog = new JTextArea();
// Create a scroll pane to hold text area
JScrollPane scrollPane = new JScrollPane(serverLog);
JPanel serverWin = new JPanel();
serverWin.add(scrollPane);
}
和另一个有:
的班级private JPanel mainWin = new JPanel();
private JPanel gridArea = new JPanel(); //hold cells
TimeServer serverWin = new TimeServer();
public class TimClient extends JFrame implements Runnable, TimeVariables
{
his.add(mainWin, BorderLayout.CENTER);
mainWin.setLayout(new BorderLayout());
// mainWin.setLayout(new GridLayout(1,2));
//Create Grid
gridArea.setLayout(new GridLayout(10, 10, 2, 2));
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
gridArea.add(cell[i][j] = new Cell(i, j, this));
gridArea.setBorder(new LineBorder(Color.black, 1));
jlblTitle.setHorizontalAlignment(JLabel.CENTER);
jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
jlblTitle.setBorder(new LineBorder(Color.black, 1));
jlblStatus.setBorder(new LineBorder(Color.black, 1));
mainWin.add(gridArea, BorderLayout.CENTER);
mainWin.add(serverWin, BorderLayout.PAGE_END);
// Place the panel and the labels to the frame
setLayout(new BorderLayout()); // implicit anyway
add(jlblTitle, BorderLayout.NORTH);
add(mainWin, BorderLayout.CENTER);
add(jlblStatus, BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create a frame
TimClient frame = new TimClient("Time Client");
//frame.getContentPane().add(BorderLayout)
// Display the frame
frame.setSize(620, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
我想要做的就是将JPanel中的服务器日志和网格保持在一起,你能解释一下吗?
还如何将服务器类中的JPanel添加到客户端中?
这里有一幅关于我如何看待它的草图:
答案 0 :(得分:1)
上面的问题似乎源于一个糟糕的Swing代码实践,似乎是由Swing代码生成器强化的(虽然我不确定你目前是否正在使用这个工具)和官方的Swing教程,以及是:
所以我建议你这样做:
如果您已经完成了将GUI代码与逻辑代码分离的好工作,那么在保持相同逻辑(或“模型”)代码的同时重新编写GUI代码应该很容易。
编辑2
关于您已更改的问题,现在第一个类扩展了JPanel,只需将JPanel添加到BorderLayout.LINE_END结尾(也称为BorderLayout.EAST)位置的JFrame中。