- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在调整 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 会占用它们周围的额外空间。默认情况下,填充设置为 0,所以我不确定为什么它会在它们周围创建一个边框。
谢谢。
为纪尧姆编辑:
我知道如果我想将它作为独立运行,则 main 是必需的,它目前是另一个应用程序的一部分,并从它内部单独调用。为了完整性而在没有 main 的情况下发布它可能不是最好的决定,但无论如何,这不是什么大问题。
你的代码把图片实际显示的部分去掉了。 “文件路径”应该被替换为实际的图像文件路径,因此您可以显示图像。当它只是文本时,很难真正看到手头的问题。
当您正确显示图像并尝试调整整个 GUI 的大小时,您会注意到图像没有缩小。它保持其首选大小,并且当它变得小于它时会出现问题。在我的代码中,当它不能至少显示首选尺寸时,它会完全停止显示图片。在您的代码中,它在缩小时切断了部分图片。
我想要的是它实际调整包含图片的 JLabel 的大小,以便随着 GUI 调整大小,图像也会调整大小。对于我的程序,图像有必要相应地缩小。用户最终将拥有基于单击图像的功能,因此仅显示部分图像或部分图像根本行不通。
请再次尝试你的代码/我的代码并重现我遇到的问题,用图片测试,以便我们找到正确的解决方案。谢谢。
最佳答案
JFrame.setVisible(true)
应该是您调用的最后一行。GridBagLayout
可以根据框架大小调整组件的大小,只要您使用 weightx/weighty
和 fill
<更新:
anchor
和/或 fill
frame.pack()
调整框架的大小setExtendedState()
最大化帧顺便说一句,SSCCE意味着您为其他人制作了一个可运行的示例,这包括将图像的本地路径更改为在线 URL(请参见下面的示例)。
使用以下代码片段,一切似乎都正常:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;
public class DraftGUI implements MouseListener {
private static final String IMAGE_URL = "http://images.paramountbusinessjets.com/space/spaceshuttle.jpg";
private JPanel jpPack;
private JPanel jpCards;
private JPanel jpInfo;
private JPanel jpChat;
private TextField tf;
private StyledDocument doc;
private JTextPane tp;
private JScrollPane sp;
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
public DraftGUI() throws MalformedURLException {
final JFrame frame = new JFrame("Draft");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the content pane, we'll add everything to it and then add it to the frame
JPanel contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
// creates some panels with some default values for now
JPanel jpCards = new JPanel(new BorderLayout());
jpCards.setBackground(Color.BLUE);
JPanel jpInfo = new JPanel();
jpInfo.setBackground(Color.GREEN);
JPanel jpPack = new JPanel(new GridBagLayout());
jpPack.setBackground(Color.RED);
// grab some info to make the JTextPane and make it scroll
tf = new TextField();
doc = new DefaultStyledDocument();
tp = new JTextPane(doc);
tp.setEditable(false);
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);
// 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.weightx = 1.0;
d.weighty = 1.0;
d.fill = GridBagConstraints.BOTH;
d.gridx = 0;
d.gridy = 0;
ImageLabel imageLabel1 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
jpPack.add(imageLabel1, d);
ImageLabel imageLabel2 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
d.gridx++;
jpPack.add(imageLabel2, d);
ImageLabel imageLabel3 = new ImageLabel(new ImageIcon(new URL(IMAGE_URL)));
d.gridy++;
jpPack.add(imageLabel3, d);
imageLabel1.addMouseListener(this);
imageLabel2.addMouseListener(this);
imageLabel3.addMouseListener(this);
frame.pack();
// 223, 310 are the aspect values for a card image, width, height
// these need to be maintained as the GUI size changes
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new DraftGUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
public static class ImageLabel extends JPanel {
private static int counter = 1;
private ImageIcon icon;
private int id = counter++;
public ImageLabel(ImageIcon icon) {
super();
setOpaque(false);
this.icon = icon;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(icon.getIconWidth(), icon.getIconHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
double zoom = Math.min((double) getWidth() / icon.getIconWidth(), (double) getHeight() / icon.getIconHeight());
int width = (int) (zoom * icon.getIconWidth());
int height = (int) (zoom * icon.getIconHeight());
g.drawImage(icon.getImage(), (getWidth() - width) / 2, (getHeight() - height) / 2, width, height, this);
g.setFont(g.getFont().deriveFont(36.0f));
g.drawString(String.valueOf(id), getWidth() / 2, getHeight() / 2);
}
}
}
关于java - 在框架调整大小 Java 上调整组件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13040747/
我需要开发一个简单的网站,我通常使用 bootstrap CSS 框架,但是我想使用 Gumbyn,它允许我使用 16 列而不是 12 列。 我想知道是否: 我可以轻松地改变绿色吗? 如何使用固定布局
这个问题在这里已经有了答案: 关闭 13 年前。 与直接编写 PHP 代码相比,使用 PHP 框架有哪些优点/缺点?
我开发了一个 Spring/JPA 应用程序:服务、存储库和域层即将完成。 唯一缺少的层是网络层。我正在考虑将 Playframework 2.0 用于 Web 层,但我不确定是否可以在我的 Play
我现有的 struts Web 应用程序具有单点登录功能。然后我将使用 spring 框架创建一个不同的 Web 应用程序。然后想要使用从 struts 应用程序登录的用户来链接新的 spring 应
我首先使用Spark框架和ORMLite处理网页上表单提交的数据,在提交中文字符时看到了unicode问题。我首先想到问题可能是由于ORMLite,因为我的MySQL数据库的字符集已设置为使用utf8
我有一个使用 .Net 4.5 功能的模块,我们的应用程序也适用于 XP 用户。所以我正在考虑将这个 .net 4.5 依赖模块移动到单独的项目中。我怎样才能有一个解决方案,其中有两个项目针对不同的版
我知道这是一个非常笼统的问题,但我想我并不是真的在寻找明确的答案。作为 PHP 框架的新手,我很难理解它。 Javascript 框架,尤其是带有 UI 扩展的框架,似乎通过将 JS 代码与设计分开来
我需要收集一些关于现有 ORM 解决方案的信息。 请随意编写任何编程语言。 你能谈谈你用过的最好的 ORM 框架吗?为什么它比其他的更好? 最佳答案 我使用了 NHibernate 和 Entity
除了 Apple 的 SDK 之外,还有什么强大的 iPhone 框架可供开始开发?有没有可以加快开发时间的方法? 最佳答案 此类框架最大的是Three20 。 Facebook 和许多其他公司都使用
有人可以启发我使用 NodeJS 的 Web 框架吗?我最近开始从免费代码营学习express js,虽然一切进展顺利,但我对express到底是什么感到困惑。是全栈框架吗?纯粹是为了后端吗?我发现您
您可以推荐哪种 Ajax 框架/工具包来构建使用 struts 的 Web 应用程序的 GUI? 最佳答案 我会说你的 AJAX/javascript 库选择应该较少取决于你的后端是如何实现的,而更多
我有生成以下错误的 python 代码: objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.
首先,很抱歉,如果我问的问题很明显,因为我没有编程背景,那我去吧: 我想运行一系列测试场景并在背景部分声明了几个变量(我打印它们以仔细检查它们是否已正确声明),第一个是整数,另外两个字符串为你可以看到
在我们承担的一个项目中,我们正在寻找一个视频捕获和录制库。我们的基础工作(基于 google 搜索)表明 vlc (libvlc)、ffmpeg (libavcodec) 和 gstreamer 是三
我试过没有运气的情况下寻找某种功能来杀死/中断Play中的正常工作!框架。 我想念什么吗?还是玩了!实际没有添加此功能? 最佳答案 Java stop类中没有像Thread方法那样的东西,由于种种原因
我们希望在我们的系统中保留所有重大事件的记录。例如,在数据库可能存储当前用户状态的地方,事件日志应记录对该状态的所有更改以及更改发生的时间。 事件记录工具应该尽可能接近于事件引发器的零开销,应该容纳结
那里有 ActionScript 2.0/3.0 的测试框架列表吗? 最佳答案 2010-05-18 更新 由于这篇文章有点旧,而且我刚刚收到了赞成票,因此可能值得提供一些更新的信息,这样人们就不会追
我有一个巨大的 numpy 数组列表(一维),它们是不同事件的时间序列。每个点都有一个标签,我想根据其标签对 numpy 数组进行窗口化。我的标签是 0、1 和 2。每个窗口都有一个固定的大小 M。
我是 Play 的新手!并编写了我的第一个应用程序。这个应用程序有一组它依赖的 URL,从 XML 响应中提取数据并返回有效的 URL。 此应用程序需要在不同的环境(Dev、Staging 和 Pro
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我是一名优秀的程序员,十分优秀!