java的框架大小_在框架上调整Java组件的大小

调整GUI大小时,尝试调整GUI中的组件大小时遇到??麻烦.现在,当我调整GUI的大小时,组件的大小没有改变,它们保持为设置的静态大小.当我调整GUI大小并通过显示它们所需的最小尺寸时,它们将不再显示.我希望它们在调整GUI大小时调整大小并保持宽高比.

这是我用于GUI的代码:

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.awt.image.ImageObserver;

import java.io.*;

import java.net.*;

import javax.imageio.ImageIO;

import javax.swing.*;

import javax.swing.border.Border;

import javax.swing.text.*;

import javax.swing.text.html.*;

public class DraftGUI implements MouseListener {

private JPanel jpPack;

private JPanel jpCards;

private JPanel jpInfo;

private JPanel jpChat;

private TextField tf;

private StyledDocument doc;

private JTextPane tp;

private JScrollPane sp;

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseClicked(MouseEvent e) {

e.getComponent().setVisible(false);

}

private Client client;

public GUI(Client client) throws IOException {

JFrame frame = new JFrame("Draft");

//set the size to fullscreen to start

frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);

//set the content pane, we'll add everything to it and then add it to the frame

JPanel contentPane = new JPanel();

contentPane.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);

contentPane.setLayout(new GridBagLayout());

//creates some panels with some default values for now

JPanel jpCards = new JPanel(new BorderLayout());

jpCards.setOpaque(true); //ensures it paints every pixel

jpCards.setBackground(Color.BLUE);

JPanel jpInfo = new JPanel();

jpInfo.setOpaque(true);

jpInfo.setBackground(Color.GREEN);

JPanel jpPack = new JPanel(new GridBagLayout());

jpPack.setOpaque(true);

jpPack.setBackground(Color.RED);

//grab some info to make the JTextPane and make it scroll

this.client = client;

tf = new TextField();

doc = new DefaultStyledDocument();

tp = new JTextPane(doc);

tp.setEditable(false);

tf.addActionListener(this.client);

sp = new JScrollPane(tp);

//adding the quantities to the chat panel

JPanel jpChat = new JPanel();

jpChat.setLayout(new BorderLayout());

jpChat.add("North", tf);

jpChat.add("Center", sp);

//a new GridBagConstraints used to set the properties/location of the panels

GridBagConstraints c = new GridBagConstraints();

//adding some panels to the content pane

//set it to start from the top left of the quadrant if it's too small

c.anchor = GridBagConstraints.FIRST_LINE_START;

c.fill = GridBagConstraints.BOTH; //set it to fill both vertically and horizontally

c.gridx = 0; //set it to quadrant x=0 and

c.gridy = 0; //set it to quadrant y=0

c.weightx = 0.7;

c.weighty = 0.3;

contentPane.add(jpCards, c);

c.gridx = 1;

c.gridy = 0;

c.weightx = 0.3;

c.weighty = 0.3;

contentPane.add(jpInfo, c);

c.gridx = 0;

c.gridy = 1;

c.weightx = 0.7;

c.weighty = 0.7;

contentPane.add(jpPack, c);

c.gridx = 1;

c.gridy = 1;

c.weightx = 0.3;

c.weighty = 0.7;

contentPane.add(jpChat, c);

//set some necessary values

frame.setContentPane(contentPane);

frame.setLocationByPlatform(true);

frame.setVisible(true);

//This code works for adding an Image

//need to learn how to specify a path not dependent on the specific users's machine

//this is not a high priority for now though

GridBagConstraints d = new GridBagConstraints();

d.gridx = 0;

d.gridy = 0;

ImageLabel imageLabel1 = new ImageLabel("path-to-file");

imageLabel1.setPreferredSize(new Dimension(223, 310));

jpPack.add(imageLabel1, d);

ImageLabel imageLabel2 = new ImageLabel("path-to-file");

imageLabel2.setPreferredSize(new Dimension(223, 310));

ImageLabel imageLabel3 = new ImageLabel("path-to-file");

imageLabel3.setPreferredSize(new Dimension(223, 310));

d.gridx = 1;

jpPack.add(imageLabel2, d);

d.gridy = 1;

jpPack.add(imageLabel3, d);

imageLabel1.addMouseListener(this);

imageLabel2.addMouseListener(this);

imageLabel3.addMouseListener(this);

//223, 310 are the aspect values for a card image, width, height

//these need to be maintained as the GUI size changes

}

}

class ImageLabel extends JLabel {

Image image;

ImageObserver imageObserver;

// constructor with filename

ImageLabel(String filename) {

ImageIcon icon = new ImageIcon(filename);

image = icon.getImage();

imageObserver = icon.getImageObserver();

}

// constructor with icon

ImageLabel(ImageIcon icon) {

image = icon.getImage();

imageObserver = icon.getImageObserver();

}

// overload setIcon method

void setIcon(ImageIcon icon) {

image = icon.getImage();

imageObserver = icon.getImageObserver();

}

// overload paint()

public void paint( Graphics g ) {

super.paint( g );

g.drawImage(image, 0 , 0 , getWidth() , getHeight() , imageObserver);

}

}

由于某种原因,框架的组件没有默认的调整大小属性,因此,当框架调整大小时,组件不会执行任何操作.我不确定自己做错了什么或错过了什么,但显然我遗漏了一些东西.

另外,如果有人知道,ImageLabel会占用它们周围的额外空间.默认情况下,padding设置为0,所以我不确定为什么在边框周围创建边框.

谢谢.

编辑纪尧姆:

我知道一个主程序是必需的,如果我想将其作为一个独立的程序运行,它目前是另一个应用程序的一部分,并且可以从其中单独调用.没有完整的主体发布它可能不是最好的决定,但是无论如何,这不是一个大问题.

您的代码删除了实际显示图片的部分.应该将“文件路径”替换为实际的图像文件路径,以便显示图像.当其中只是文本时,很难真正看到手头的问题.

当正确显示图像并尝试调整整个GUI的大小时,您会注意到该图像不会缩小.它保持其首选大小,并且变得比其小时会出现问题.在我的代码中,当无法显示至少所需的尺寸时,它将完全不显示图片.在您的代码中,缩小时会切掉部分图片.

我想要的是实际调整包含图片的JLabel的大小,以便在GUI调整大小的同时,图像也要调整大小.对于我的程序,图像必须相应缩小.用户最终将具有基于单击图像的功能,因此仅显示某些图像或其中的一部分根本不会.

请重试您的代码/我的代码,并重现我遇到的问题,并用图片进行测试,以便我们找到正确的解决方案.谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值