如何在JFrame中使单个JPanel更大
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在JFrame中使单个JPanel更大相关的知识,希望对你有一定的参考价值。
我正在制作游戏,我的框架中有多个面板。 panel4应该包含我的滚轮,问题是panel4与主板上所有其他面板的尺寸相同,当我需要panel4要大得多时,因为我希望车轮的图片能够完全显示,因为它的立场,当我需要全部显示时,只显示轮子的顶部。
public static void setUp() throws IOException {
frame=new JFrame();
frame.setLayout(new GridLayout(7,1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(290, 5);
frame.setSize(1100, 1100);
frame.setTitle("Wheel of fortune");
final JPanel scorePanel=new JPanel();
final JPanel namePanel=new JPanel();
final JPanel panel=new JPanel(new GridLayout(1,3,1,1));
final JPanel panel1=new JPanel();
final JPanel panel2=new JPanel();
final JPanel panel3=new JPanel(new GridLayout(2,1,1,1));
panel3.add(new JLabel("Dead letters/phrases:"));
panel3.add(LettersOrPhGuessed);
scorePanel.add(new JLabel("Score Board:"));
final WheelGui test = new WheelGui();
final JPanel panel4=test;
panel4.setSize(800, 600);//this does nothing in the program
namePanel.add(playerName);
frame.getContentPane().add(namePanel);
frame.getContentPane().add(scorePanel);
frame.getContentPane().add(panel);
frame.getContentPane().add(panel4);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel3);
frame.setVisible(true);
}
这是我构建panel4的精髓所在。我试图让这个面板更大,但当我这样做时,屏幕上似乎没有任何事情发生。任何建议都会很棒。
@SuppressWarnings("serial")
public class WheelGui extends JPanel{
ImageIcon image = new ImageIcon("WheelofFortune.JPG");
JPanel rotationPanel;
final int WIDTH = 800;
final int HEIGHT = 600;
static double degrees;
public WheelGui()
{
super();
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
setBackground(Color.lightGray);
setLayout(null);
setFocusable(true);
rotationPanel = new JPanel();
rotationPanel = new turningCanvas();
rotationPanel.setPreferredSize(new Dimension(image.getIconWidth(),image.getIconHeight()));
//rotationPanel.setSize(800,600);
add(rotationPanel);
rotationPanel.setBounds(WIDTH/8, HEIGHT/8, rotationPanel.getPreferredSize().width, rotationPanel.getPreferredSize().height);
degrees = 0;
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
}
public class turningCanvas extends JPanel
{
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(degrees),image.getIconWidth()/2,image.getIconHeight()/2);
image.paintIcon(this, g2d, 0, 0);
}
}
public void rotate(){
for(int i=0;i<360;i+=4){
degrees++;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
你有很多问题......
首先,你正在使用GridLayout
。 GridLayout
将布置网格图案中的所有组件,并根据父容器的宽度和高度为每个组件分配相同的空间量。如果您希望组件具有不同的尺寸,GridLayout
不是最佳选择。
其次,你正在使用setPreferredSize
。这是一种不好的做法,因为您没有防范应用程序的某些其他部分可能会尝试稍后更改组件的大小。
相反,你应该尽可能地覆盖getPreferredSize
方法。
就个人而言,我会推荐一个GridBagLayout
,但你可能想看看A Visual Guide to Layout Managers的其他选择
以示例更新
我知道你要问的唯一方法就是使用GridBagLayout
,这使你可以灵活地定义单个组件从可用空间中占用多少空间。它是掌握最灵活,最困难的布局管理者之一......
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class TestLayout25 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
setUp();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public static void setUp() throws IOException {
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setTitle("Wheel of fortune");
final JPanel scorePanel = new JPanel();
final JPanel namePanel = new JPanel();
final JPanel panel = new JPanel(new GridLayout(1, 3, 1, 1));
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
final JPanel panel3 = new JPanel(new GridLayout(2, 1, 1, 1));
panel3.add(new JLabel("Dead letters/phrases:"));
panel3.add(createPane(Color.RED));
scorePanel.add(new JLabel("Score Board:"));
final WheelGui test = new WheelGui();
final JPanel panel4 = test;
panel4.setSize(800, 600);//this does nothing in the program
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
namePanel.add(new JLabel("Blue"), gbc);
frame.getContentPane().add(namePanel, gbc);
frame.getContentPane().add(scorePanel, gbc);
frame.getContentPane().add(panel, gbc);
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
frame.getContentPane().add(panel4, gbc);
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.getContentPane().add(panel2, gbc);
frame.getContentPane().add(panel1, gbc);
frame.getContentPane().add(panel3, gbc);
frame.setVisible(true);
}
protected static JPanel createPane(Color color) {
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(color));
return panel;
}
public static class WheelGui extends JPanel {
ImageIcon image = new ImageIcon("WheelofFortune.JPG");
JPanel rotationPanel;
final int WIDTH = 800;
final int HEIGHT = 600;
static double degrees;
public WheelGui() {
super();
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.lightGray);
setLayout(null);
setFocusable(true);
rotationPanel = new JPanel();
rotationPanel = new turningCanvas();
rotationPanel.setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight()));
//rotationPanel.setSize(800,600);
add(rotationPanel);
rotationPanel.setBounds(WIDTH / 8, HEIGHT / 8, rotationPanel.getPreferredSize().width, rotationPanel.getPreferredSize().height);
degrees = 0;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
public class turningCanvas extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(degrees), image.getIconWidth() / 2, image.getIconHeight() / 2);
image.paintIcon(this, g2d, 0, 0);
}
}
public void rotate() {
for (int i = 0; i < 360; i += 4) {
degrees++;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
您正在使用GridLayout。布局考虑了每个组件的大小和位置,你几乎无能为力。
GridLayout对象将组件放置在单元格网格中。每个组件占用其单元中的所有可用空间,并且每个单元的大小完全相同。
您将不得不使用另一种布局。 GridBagLayout可能很有用。
以上是关于如何在JFrame中使单个JPanel更大的主要内容,如果未能解决你的问题,请参考以下文章
我如何编写代码来显示每个 JPanel 中的每个字母,以及如何旋转。 (JFrame、NetBeans)