gpt4 book ai didi

java - 我的 swing 应用程序中的边框布局出了什么问题?

转载 作者:行者123 更新时间:2023-11-30 06:34:00 26 4
gpt4 key购买 nike

我有一个简单的应用程序,我想在可滚动面板中显示大图像。我正在使用来自 http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html 的 NavigableImagePanel

首先是我得到的结果 - Rotate and Resize

图像目前是顶部按钮附近的一个非常小的面板。这是 BorderLayout.CENTER

NavigableImagePanel 的代码:

http://pastebin.com/1wHRwMJU

和我的 OpenImage.java 代码:

import java.awt.*;
import java.awt.event.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class OpenImage extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 9066218264791891436L;
Image img;

public OpenImage() throws IOException{
super("Resize and Rotate");

setDefaultCloseOperation(EXIT_ON_CLOSE);
BorderLayout grid = new BorderLayout();

//this.setLayout(grid);
setSize(900,700);
setVisible(true);


//Row 1 holds some important buttons
//FlowLayout layout1 = new FlowLayout();
//MigLayout mig = new MigLayout();
JPanel row1 = new JPanel();
//LayoutManager grid = new BoxLayout(row1, BoxLayout.X_AXIS);
//row1.setLayout(grid);
//row1.setLayout(BorderLayout.NORTH);
//row1.setMaximumSize(new Dimension(100,100));
BorderLayout border = new BorderLayout();
//row1.setPreferredSize(new Dimension(0, 400));

JButton open = new JButton ("Open");
open.addActionListener(this);
JButton rotate = new JButton("Rotate");
rotate.addActionListener(this);
JButton resize = new JButton("Resize");
resize.addActionListener(this);
JButton exit = new JButton ("Exit");
exit.addActionListener(this);
row1.add(open);
row1.add(rotate);
row1.add(resize);
row1.add(exit);




//This section has a workable picture panel, but it is too large.
//ImagePanel imagepanel = new ImagePanel();
BorderLayout grid1 = new BorderLayout();
Container cp = getContentPane();
cp.setLayout(grid1);

//add(row1);

//JScrollPane row2 = new JScrollPane(imagepanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);


BufferedImage img = ImageIO.read(new File("/home/adam/snow.jpg"));

NavigableImagePanel imagepanel = new NavigableImagePanel(img);

JPanel row2 = new JPanel();
row2.add(imagepanel);
//row2.repaint();
JButton save = new JButton("Save");
JPanel row3 = new JPanel();
row3.add(save);

cp.add(BorderLayout.NORTH, row1);
cp.add(BorderLayout.CENTER, row2);
cp.add(BorderLayout.SOUTH, row3);


}




public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == "Exit"){
System.exit(0);
}
if (command == "Open"){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);


}
}



//This method below is now being deprecat


public static void main(String [] args){
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
JFrame frame = new OpenImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

最佳答案

  1. 您在此处发布的代码缺少大量 Java 导入,在这里看不到任何来自 Swing 包的 Java 导入,以及为 Image 导入。
  2. setVisible(true); 必须是构造函数中的最后一行代码。
  3. 在任何情况下,setBackground() 都可以帮助您发现任何问题。
  4. 在所有情况下尝试使用 pack() 而不是 setSize(900,700);
  5. 因为我设置了 imagepanel.setPreferredSize(new Dimension(600, 400));,所以 JComponents 必须返回首选大小是错误的,然后 pack( ) 将正常工作。
  6. 不知道你从哪里得到这段代码,所以几乎不可能无异常(exception)地工作。
  7. 只是清理无用的烂摊子

enter image description here

来自代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class OpenImage extends JFrame implements ActionListener {

private static final long serialVersionUID = 9066218264791891436L;
private Image img;

public OpenImage() {
super("Resize and Rotate");

JButton open = new JButton("Open");
open.addActionListener(this);
JButton rotate = new JButton("Rotate");
rotate.addActionListener(this);
JButton resize = new JButton("Resize");
resize.addActionListener(this);
JButton exit = new JButton("Exit");
exit.addActionListener(this);

JPanel row1 = new JPanel();
row1.setBackground(Color.red);
row1.setLayout(new FlowLayout());
row1.setBorder(new LineBorder(Color.black, 1));
row1.add(open);
row1.add(rotate);
row1.add(resize);
row1.add(exit);

JPanel imagepanel = new JPanel();
imagepanel.setLayout(new BorderLayout());
imagepanel.setBackground(Color.blue);
imagepanel.setBorder(new LineBorder(Color.black, 1));
imagepanel.setPreferredSize(new Dimension(600, 400));

JPanel row2 = new JPanel();
row2.setLayout(new BorderLayout(10, 10));
row2.setBorder(new LineBorder(Color.black, 1));
row2.add(imagepanel, BorderLayout.CENTER);
row2.setBackground(Color.red);
JButton save = new JButton("Save");

JPanel row3 = new JPanel();
row3.setBorder(new LineBorder(Color.black, 1));
row3.setBackground(Color.green);
row3.setLayout(new FlowLayout());
row3.add(save);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
add(BorderLayout.NORTH, row1);
add(BorderLayout.CENTER, row2);
add(BorderLayout.SOUTH, row3);
pack();
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == "Exit") {
System.exit(0);
}
if (command == "Open") {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
OpenImage openImage = new OpenImage();
}
});
}
}

关于java - 我的 swing 应用程序中的边框布局出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com