gpt4 book ai didi

java - 如何在 JFrame 中分隔面板以分隔类

转载 作者:行者123 更新时间:2023-12-02 09:30:19 24 4
gpt4 key购买 nike

我有一个名为 MainFrame 的 JFrame,我试图将每个面板分成它自己的类。我尝试过,但由于一些错误而失败。

请查看我尝试过的代码。什么失败了,我做错了什么?

public class MainFrame extends JFrame {

static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

private StartScreenPlayerPanel startScreenPlayerPanel;

private JPanel contentPane;
private JTable table;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
System.out.println(screenSize);
frame.setMinimumSize(new Dimension(screenSize.width/2, screenSize.height/2));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MainFrame() {

startScreenPlayerPanel = new StartScreenPlayerPanel();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, (screenSize.width * 2 / 3), (screenSize.height * 2 / 3));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{1200, 0};
gbl_contentPane.rowHeights = new int[]{74, 0, 446, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

JLabel lblTitle = new JLabel("The Coin Game",SwingConstants.CENTER);
lblTitle.setFont(new Font("Arial", Font.PLAIN, (int)screenSize.width/30));
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.gridwidth = 2;
gbc_lblTitle.insets = new Insets(0, 0, 5, 0);
gbc_lblTitle.anchor = GridBagConstraints.NORTH;
gbc_lblTitle.fill = GridBagConstraints.HORIZONTAL;
gbc_lblTitle.gridx = 0;
gbc_lblTitle.gridy = 0;
contentPane.add(lblTitle, gbc_lblTitle);

JPanel StartScreenBtnPanel = new JPanel();
GridBagConstraints gbc_StartScreenBtnPanel = new GridBagConstraints();
gbc_StartScreenBtnPanel.gridwidth = 0;
gbc_StartScreenBtnPanel.insets = new Insets(0, 0, 5, 0);
gbc_StartScreenBtnPanel.fill = GridBagConstraints.BOTH;
gbc_StartScreenBtnPanel.gridx = 0;
gbc_StartScreenBtnPanel.gridy = 1;
contentPane.add(StartScreenBtnPanel, gbc_StartScreenBtnPanel);
StartScreenBtnPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

JButton btnAddPlayer = new JButton("Add Player");
btnAddPlayer.setFont(new Font("Tahoma", Font.PLAIN, 16));
StartScreenBtnPanel.add(btnAddPlayer);

JButton btnStartGame = new JButton("Start Game");
btnStartGame.setFont(new Font("Tahoma", Font.PLAIN, 16));
StartScreenBtnPanel.add(btnStartGame);

我试图将代码分离到不同的类

//      JPanel tblPanel = new JPanel();
// GridBagConstraints gbc_tblPanel = new GridBagConstraints();
// gbc_tblPanel.gridwidth = 2;
// gbc_tblPanel.insets = new Insets(0, 0, 5, 0);
// gbc_tblPanel.fill = GridBagConstraints.BOTH;
// gbc_tblPanel.gridx = 0;
// gbc_tblPanel.gridy = 2;
// contentPane.add(tblPanel, gbc_tblPanel);
// tblPanel.setLayout(new BorderLayout(0, 0));

// table = new JTable();
// tblPanel.add(table.getTableHeader(), BorderLayout.NORTH);
// table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
// table.setModel(new DefaultTableModel(
// new Object[][] {
// },
// new String[] {
// "New Player", "Initial Points"
// }
// ) {
// Class[] columnTypes = new Class[] {
// String.class, Integer.class
// };
// public Class getColumnClass(int columnIndex) {
// return columnTypes[columnIndex];
// }
// });
// table.getColumnModel().getColumn(0).setResizable(false);
// table.getColumnModel().getColumn(0).setMinWidth(14);
// tblPanel.add(table, BorderLayout.CENTER);

我尝试了什么

MainFrame类的末尾

contentPane.add(startScreenPlayerPanel, startScreenPlayerPanel.getSSPPConstraints());   
}
public StartScreenPlayerPanel getStartScreenPlayerPanel() {
return startScreenPlayerPanel;
}

新类 - StartScreenPlayerPanel

public class StartScreenPlayerPanel extends JFrame {
MainFrame mainframe;

private JTable table;
private GridBagConstraints gbc_tblPanel = new GridBagConstraints();

public StartScreenPlayerPanel() {
JPanel tblPanel = new JPanel();

gbc_tblPanel.gridwidth = 2;
gbc_tblPanel.insets = new Insets(0, 0, 5, 0);
gbc_tblPanel.fill = GridBagConstraints.BOTH;
gbc_tblPanel.gridx = 0;
gbc_tblPanel.gridy = 2;
tblPanel.setLayout(new BorderLayout(0, 0));

table = new JTable();
tblPanel.add(table.getTableHeader(), BorderLayout.NORTH);
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
table.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "New Player", "Initial Points" }) {
Class[] columnTypes = new Class[] { String.class, Integer.class };

public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(0).setMinWidth(14);
tblPanel.add(table, BorderLayout.CENTER);


}

public GridBagConstraints getSSPPConstraints(){
return gbc_tblPanel;
}
}

最佳答案

I'm trying to seperate each panel to it's own class

public class StartScreenPlayerPanel extends JFrame {

好的,那么为什么要扩展 JFrame?

如果您想向主框架添加面板,那么您可以扩展JPanel:

public class StartScreenPlayerPanel extends JPanel {

现在,因为您的类"is"JPanel,所以您只需设置面板的布局管理器并向其添加组件即可。

public StartScreenPlayerPanel() 
{
setLayout(new BorderLayout());

table = new JTable();
table.setModel( ... );

add(table.getTableHeader(), BorderLayout.NORTH);
add(table, BorderLayout.CENTER);
}

此类中不需要 getSSPPConstraints() 方法。 “StartScreenPlayerPanel”不知道也不关心面板是如何使用的。它只关心自己类中组件的布局。

结果是一个 JPanel,其中添加了一个 JTable。

注意:通常在使用 JTable 时您会使用:

//add(table.getTableHeader(), BorderLayout.NORTH);
//add(table, BorderLayout.CENTER);
add(new JScrollPane(table), BorderLayout.CENTER);

滚动 Pane 会自动添加表格标题。

现在,在主类中,将面板添加到内容 Pane :

//contentPane.add(startScreenPlayerPanel, startScreenPlayerPanel.getSSPPConstraints()); 
GridBagConstraints gbc_playerPanel = new GridBagConstraints();
gbc_playerPanel.gridwidth = 2;
gbc_playerPanel.insets = new Insets(0, 0, 5, 0);
gbc_playerPanel.fill = GridBagConstraints.BOTH;
gbc_playerPanel.gridx = 0;
gbc_playerPanel.gridy = 2;
contentPane.add(startScreenPlayeerPanel, gbc_playerPanel).

这就是约束是将面板添加到内容面板的类的属性。

关于java - 如何在 JFrame 中分隔面板以分隔类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58040655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com