gpt4 book ai didi

java - 我需要将面板拆分两次,但我不知道该怎么做

转载 作者:行者123 更新时间:2023-12-01 14:34:36 25 4
gpt4 key购买 nike

好的,我有一个尺寸为 1280x720 的框架。我需要将其拆分并在左侧创建 1000x720 的一侧,在右侧创建 280x720 的一侧。现在的面板是 1000x720,我需要再次将其分割为顶部 1000x520,底部 1000x200。我已经尝试了相当长一段时间了。如果您有任何可以帮助或分享您的经验的链接,那将会非常有帮助。所以它有 3 个区域:#1 1000x520。 #3 280x720 #2 1000x200

    import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

public class GamePanel extends JFrame{


private static final long serialVersionUID = 1L;
public JSplitPane secondSplit;
SplitTableHand splitTableHand = new SplitTableHand();
ChatPanel chatPanel = new ChatPanel();


public GamePanel() {

secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel);

secondSplit.setOneTouchExpandable(false);
secondSplit.setDividerLocation(1000);
this.setSize(1280, 720);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.pack();
this.setVisible( true );
getContentPane().add( secondSplit );



}
public static void main( String args[] ){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GamePanel mainFrame = new GamePanel();

}
});


}


}

import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JSplitPane;


public class SplitTableHand extends JPanel{

/**
*
*/
private static final long serialVersionUID = 1L;
JSplitPane splitPane;
TablePanel tablePanel = new TablePanel();
HandPanel handPanel = new HandPanel();

public SplitTableHand() {


splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel, handPanel);

splitPane.setOneTouchExpandable(false);
splitPane.setDividerLocation(550);
splitPane.setPreferredSize(new Dimension(1000, 720));
add(splitPane);
splitPane.setVisible(true);
}


}

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class TablePanel extends JPanel{

private static final long serialVersionUID = 1L;
private JLabel label = new JLabel("LABEL");

public TablePanel() {
setLayout(new BorderLayout());// we shall use absolute positioning for this
this.setSize(1000, 520);
this.setPreferredSize(getSize());
this.setBackground(new Color(100,100,100)) ;
this.add(label);
this.setVisible(true);
}
}

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JPanel;


public class HandPanel extends JPanel{

private static final long serialVersionUID = 1L;
JButton button;
public HandPanel() {
//default layout is flowlayout
this.setSize(1000, 200);
this.setPreferredSize(getSize());
this.setBackground(new Color(150,150,50)) ;

for( int i = 0 ; i < 20;i++){
String name = "Button"+ i;
button = new JButton(name);
this.add(button);
this.setVisible(true);
}
}

}

import java.awt.Color;
import java.awt.FlowLayout;



import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ChatPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea chatArea = new JTextArea(10, 30);
private JTextField chatField = new JTextField(50);
JScrollPane scrollPane = new JScrollPane(chatArea);
JButton button ;

public ChatPanel() {
setLayout(new FlowLayout());
this.setSize(720, 280);
this.setPreferredSize(getSize());
this.setBackground(new Color(50,50,50)) ;
chatArea.setEditable(false);
chatArea.setLineWrap(true);
chatArea.setVisible(true);
chatField.setVisible(true);
button = new JButton("Button");
button.setEnabled(true);
button.setVisible(true);

this.add(scrollPane);
this.add(chatField);
this.setVisible(true);
this.add(button);
}

}

最佳答案

首先,您的程序出现编译错误。修复它们。您必须使用 JPanel 扩展 SplitTableHand。这样,您在 GamePanel 中的错误就得到了解决。

public class SplitTableHand extends JPanel

第二点是在 GamePanel 类构造函数中,您创建了 topPanel 并将其添加到框架中,而不是添加 secondSplit

GamePanel的构造函数更改为

public GamePanel() {
secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel);

secondSplit.setOneTouchExpandable(true);
secondSplit.setDividerLocation(150);
this.setSize(1280, 720);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.pack();
this.setVisible( true );
getContentPane().add( secondSplit );
}

SplitTableHand 类中,您还必须添加 splitPane

在其构造函数中添加一条语句add(splitPane);

关于java - 我需要将面板拆分两次,但我不知道该怎么做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16594948/

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