JFrame 没有显示所有内容?必须调整大小才能显示内容
JFrame not displaying all the content? Have to resize to show the content
我正在尝试构建一个简单的 GUI 界面。我已经使用 paintComponent 方法向 JPanel 添加了背景图像。
问题是构建输出时它只显示一个小的 window,如下所示:
我必须调整输出大小 window 以显示完整图像。如何使图像适合 window?
这是我的新源代码:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JFrame {
public test(){
super("Staff Management");
this.setContentPane(new staff());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.pack();
}
public class staff extends JPanel{
private ImageIcon i;
public staff() {
i = new ImageIcon("D:\staff-directory.jpg");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(i.getIconWidth(),i.getIconHeight());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
i.paintIcon(this,g,0,0);
}
}
}
绘制组件时,您需要覆盖 getPreferedSize()
方法来定义自定义组件的大小。您可以像下面这样更改 class:
public class staff extends JPanel{
private ImageIcon i;
public staff() {
i = new ImageIcon("d:\staff.jpg");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(i.getIconWidth(),i.getIconHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
i.paintIcon(this, g, 0, 0);
}
}
也只使用 pack()
而不是 this.setSize(894,553);
。
It is just that almost all the answers on this website says to use custom painting of JPanel so I spent a day learning how to do this..
这些答案通常建议您使用 Graphics.drawImage(...) 方法绘制图像。即使你在上一个问题中得到的答案也表明了这一点,所以我不知道你为什么现在要尝试画一个图标。没有理由创建一个图标来保存图像。
if you could show me how to do this with JLabel and Icon that would be a great help
没有技巧。就像使用 JPanel:
JLabel label = new JLabel( ... );
label.setLayout( new FlowLayout() );
label.add( new JButton("one") );
label.add( new JButton("two") );
如果您不知道如何向 JLabel 添加图标,请阅读 How to Use Icons 上的 Swing 教程。
你在这里的问题中甚至得到了这个建议:,那你为什么又问这个问题?
我正在尝试构建一个简单的 GUI 界面。我已经使用 paintComponent 方法向 JPanel 添加了背景图像。
问题是构建输出时它只显示一个小的 window,如下所示:
我必须调整输出大小 window 以显示完整图像。如何使图像适合 window?
这是我的新源代码:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JFrame {
public test(){
super("Staff Management");
this.setContentPane(new staff());
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.pack();
}
public class staff extends JPanel{
private ImageIcon i;
public staff() {
i = new ImageIcon("D:\staff-directory.jpg");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(i.getIconWidth(),i.getIconHeight());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
i.paintIcon(this,g,0,0);
}
}
}
绘制组件时,您需要覆盖 getPreferedSize()
方法来定义自定义组件的大小。您可以像下面这样更改 class:
public class staff extends JPanel{
private ImageIcon i;
public staff() {
i = new ImageIcon("d:\staff.jpg");
}
@Override
public Dimension getPreferredSize() {
return new Dimension(i.getIconWidth(),i.getIconHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
i.paintIcon(this, g, 0, 0);
}
}
也只使用 pack()
而不是 this.setSize(894,553);
。
It is just that almost all the answers on this website says to use custom painting of JPanel so I spent a day learning how to do this..
这些答案通常建议您使用 Graphics.drawImage(...) 方法绘制图像。即使你在上一个问题中得到的答案也表明了这一点,所以我不知道你为什么现在要尝试画一个图标。没有理由创建一个图标来保存图像。
if you could show me how to do this with JLabel and Icon that would be a great help
没有技巧。就像使用 JPanel:
JLabel label = new JLabel( ... );
label.setLayout( new FlowLayout() );
label.add( new JButton("one") );
label.add( new JButton("two") );
如果您不知道如何向 JLabel 添加图标,请阅读 How to Use Icons 上的 Swing 教程。
你在这里的问题中甚至得到了这个建议: