JPanel EAST 和 CENTER 面板似乎重叠
JPanel EAST and CENTER panels seem to overlay
我看过很多类似的帖子,但是none似乎和我的问题一样,看起来比较简单。我有以下代码:
public PlayScreen(Player player){
this.player = player;
playScreen = new JPanel();
playScreen.setLayout(new BorderLayout());
setTowerButtons();
playScreen.add(towerButtons, BorderLayout.EAST); //line 6
setDetailLabels();
playScreen.add(detailPanel, BorderLayout.WEST);
setMap("map.png");
playScreen.add(new JButton("hello"), BorderLayout.CENTER); //line 10
playScreen.setVisible(true);
}
public class View {
private JFrame frame;
private Container contentPane;
private Player player;
private LoadScreen loadScreen;
private PlayScreen playScreen;
public View(Player player){
frame = new JFrame("Display");
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
this.player = player;
frame.setVisible(true);
frame.pack();
}
public void makeLoadScreen(){
loadScreen = new LoadScreen();
contentPane.add(loadScreen.getLoadScreen(), BorderLayout.CENTER);
frame.pack();
}
public void makePlayScreen(Player player){
contentPane.remove(loadScreen.getLoadScreen());;
playScreen = new PlayScreen(player);
contentPane.add(playScreen.getPlayScreen());
frame.pack();
}
public class LoadScreen{
private JPanel loadScreen;
public LoadScreen(){
loadScreen = new JPanel();
JButton play = new JButton("Play");
JButton load = new JButton("Load");
loadScreen.setLayout(new GridLayout(2,1,0,0));
loadScreen.add(play);
loadScreen.add(load);
play.addActionListener(new OpenActionListener());
load.addActionListener(new LoadActionListener());
}
public class OpenActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
makePlayScreen(player);
}
}
}
}
首次创建视图 class。我手动调用 makeLoadScreen() 方法(虽然我稍后会更改它以自动执行此操作),它创建了嵌套 class 中描述的 LoadScreen。这给出了两个按钮的视图,播放和加载。单击播放然后创建问题所在的 PlayScreen。
当它是 运行 时,detailPanel 总是按预期显示在左上角。但是 towerButtons 面板消失了,"hello" 按钮显示出来,填满了屏幕的其余部分。如果我切换第 6 行和第 10 行,"hello" 按钮就会消失并显示 towerButtons 面板,填满屏幕的其余部分。
相反,我希望 towerButtons 显示在右侧,"hello" 按钮位于此按钮和 detailPanel 之间。
编辑:添加了其余代码,应该早点完成,抱歉!
Edits2:问题出在 "towerButtons," 与 JPanel 的覆盖无关。
I would like the towerButtons to show on the right, with the "hello" button in between this and the detailPanel.
问题可能出在您的代码中的其他地方,所以我认为这对您没有帮助,但在这里:
public class Example extends JFrame {
Example() {
JPanel playScreen = new JPanel();
playScreen.setLayout(new BorderLayout());
JPanel towerButtons = new JPanel();
towerButtons.add(new JLabel("towerButtons"));
playScreen.add(towerButtons, BorderLayout.EAST);
JPanel detailPanel = new JPanel();
detailPanel.add(new JLabel("detailPanel"));
playScreen.add(detailPanel, BorderLayout.WEST);
playScreen.add(new JButton("hello"), BorderLayout.CENTER);
add(playScreen);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new Example();
}
}
我看过很多类似的帖子,但是none似乎和我的问题一样,看起来比较简单。我有以下代码:
public PlayScreen(Player player){
this.player = player;
playScreen = new JPanel();
playScreen.setLayout(new BorderLayout());
setTowerButtons();
playScreen.add(towerButtons, BorderLayout.EAST); //line 6
setDetailLabels();
playScreen.add(detailPanel, BorderLayout.WEST);
setMap("map.png");
playScreen.add(new JButton("hello"), BorderLayout.CENTER); //line 10
playScreen.setVisible(true);
}
public class View {
private JFrame frame;
private Container contentPane;
private Player player;
private LoadScreen loadScreen;
private PlayScreen playScreen;
public View(Player player){
frame = new JFrame("Display");
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
this.player = player;
frame.setVisible(true);
frame.pack();
}
public void makeLoadScreen(){
loadScreen = new LoadScreen();
contentPane.add(loadScreen.getLoadScreen(), BorderLayout.CENTER);
frame.pack();
}
public void makePlayScreen(Player player){
contentPane.remove(loadScreen.getLoadScreen());;
playScreen = new PlayScreen(player);
contentPane.add(playScreen.getPlayScreen());
frame.pack();
}
public class LoadScreen{
private JPanel loadScreen;
public LoadScreen(){
loadScreen = new JPanel();
JButton play = new JButton("Play");
JButton load = new JButton("Load");
loadScreen.setLayout(new GridLayout(2,1,0,0));
loadScreen.add(play);
loadScreen.add(load);
play.addActionListener(new OpenActionListener());
load.addActionListener(new LoadActionListener());
}
public class OpenActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
makePlayScreen(player);
}
}
}
}
首次创建视图 class。我手动调用 makeLoadScreen() 方法(虽然我稍后会更改它以自动执行此操作),它创建了嵌套 class 中描述的 LoadScreen。这给出了两个按钮的视图,播放和加载。单击播放然后创建问题所在的 PlayScreen。
当它是 运行 时,detailPanel 总是按预期显示在左上角。但是 towerButtons 面板消失了,"hello" 按钮显示出来,填满了屏幕的其余部分。如果我切换第 6 行和第 10 行,"hello" 按钮就会消失并显示 towerButtons 面板,填满屏幕的其余部分。
相反,我希望 towerButtons 显示在右侧,"hello" 按钮位于此按钮和 detailPanel 之间。
编辑:添加了其余代码,应该早点完成,抱歉! Edits2:问题出在 "towerButtons," 与 JPanel 的覆盖无关。
I would like the towerButtons to show on the right, with the "hello" button in between this and the detailPanel.
问题可能出在您的代码中的其他地方,所以我认为这对您没有帮助,但在这里:
public class Example extends JFrame {
Example() {
JPanel playScreen = new JPanel();
playScreen.setLayout(new BorderLayout());
JPanel towerButtons = new JPanel();
towerButtons.add(new JLabel("towerButtons"));
playScreen.add(towerButtons, BorderLayout.EAST);
JPanel detailPanel = new JPanel();
detailPanel.add(new JLabel("detailPanel"));
playScreen.add(detailPanel, BorderLayout.WEST);
playScreen.add(new JButton("hello"), BorderLayout.CENTER);
add(playScreen);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new Example();
}
}