gpt4 book ai didi

java - 滚动 Pane 不会出现,直到我单击它们并且没有可用的栏

转载 作者:行者123 更新时间:2023-11-30 02:21:33 26 4
gpt4 key购买 nike

不确定我是否将其添加到了错误的图层。可能在这方面有不少错误。我将其添加到小程序中,因为我将删除 JFrame。 JFrame 仅用于测试目的。我不知道我是否设置了滚动 Pane 错误或什么。最终目标是使用按钮向 jpanel 添加图片,并且如果图片大于面板,则能够滚动。

public class main {

public static JFrame f = new JFrame();
public static JApplet a = new JApplet();
public static JPanel p = new JPanel();
public static JButton b = new JButton();
public static JScrollPane pane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public static JLabel gameimage;
public static void main(String[] args) {

f.setSize(1000,800);

a.setBounds(0,0,1000,800);
a.setVisible(true);
a.setBackground(Color.WHITE);

p.setBounds(0, 0, 1000, 800);
p.setVisible(true);
p.setBackground(Color.WHITE);
p.setLayout(null);
p.setOpaque(true);

pane.getVerticalScrollBar();
pane.getHorizontalScrollBar();
pane.setVisible(true);

b.setBounds(955, 0, 40, 30);
b.setText("+");
b.setFont(new Font("Times Roman", Font.BOLD, 30));
b.setVisible(true);
b.setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE));

b.setBackground(Color.WHITE);
b.setForeground(Color.GREEN);

b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();

file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
file.setAcceptAllFileFilterUsed(true);

if(file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
int width;
int height;
File f = file.getSelectedFile();
try {
BufferedImage bimg = ImageIO.read(new File(f.getAbsolutePath()));
width = bimg.getWidth();
height = bimg.getHeight();
String fname = f.getAbsolutePath();
gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
gameimage.setSize(width,height);
gameimage.setOpaque(true);

a.revalidate();
a.repaint();
p.removeAll();
p.revalidate();
p.repaint();
pane.revalidate();
pane.repaint();
p.setSize(width,height);
p.add(gameimage);
p.add(b);
//p.add(pane);
a.getContentPane().add(pane);
}catch(IOException ioe) {

}


}else {
System.out.println("not working");
}
}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(a);
a.add(p);
p.add(b);
}
}

最佳答案

我不会详细讨论,而是对代码进行注释

要点是:

  • 避免使用 null 布局,它们永远不会起作用,而且 Swing API 旨在操作布局管理器 API
  • 小程序已弃用,您将无法从中获得任何 JFrame 或其他基于窗口的组件无法获得的功能

虽然我没有直接执行任务,但 static 也是一个坏主意(在您使用它的上下文中),如果可以的话尽量避免它

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {

public static JFrame f = new JFrame();
// Pointless, applets are deprecated
// Besides, this isn't how you use them
//public static JApplet a = new JApplet();
public static JPanel p = new JPanel();
public static JButton b = new JButton();
public static JScrollPane pane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public static JLabel gameimage;

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

f.setSize(1000, 800);

//a.setBounds(0, 0, 1000, 800);
//a.setVisible(true);
//a.setBackground(Color.WHITE);
// Pointless, the layout manager of the JScrollPane
// will make these choices
//p.setBounds(0, 0, 1000, 800);
p.setVisible(true);
p.setBackground(Color.WHITE);
// !! This is root of your problem !!
//p.setLayout(null);
p.setLayout(new BorderLayout());
// Pointless, it already is
//p.setOpaque(true);

// Pointless, you don't do anything with the returned value
//pane.getVerticalScrollBar();
//pane.getHorizontalScrollBar();
// Pointless, it already is
//pane.setVisible(true);
// Pointless
//b.setBounds(955, 0, 40, 30);
b.setText("+");
b.setFont(new Font("Times Roman", Font.BOLD, 30));
// Pointless
//b.setVisible(true);
b.setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE));
b.setBackground(Color.WHITE);
b.setForeground(Color.GREEN);

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();

file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
file.setAcceptAllFileFilterUsed(true);

if (file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
int width;
int height;
File f = file.getSelectedFile();
try {
BufferedImage bimg = ImageIO.read(new File(f.getAbsolutePath()));
width = bimg.getWidth();
height = bimg.getHeight();
String fname = f.getAbsolutePath();
if (gameimage != null) {
p.remove(gameimage);
}
gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
gameimage.setSize(width, height);
gameimage.setOpaque(true);

//a.revalidate();
//a.repaint();
//p.removeAll();
// Not really what you want to do right now
// Besides, if you use a null layout, it won't do anything
//p.revalidate();
//p.repaint();
//pane.revalidate();
//pane.repaint();
//p.setSize(width, height);
p.add(gameimage);
//p.add(b);
//p.add(pane);
// Presumaly, pane should alredy be added to a container
//a.getContentPane().add(pane);
p.revalidate();
p.repaint();
} catch (IOException ioe) {

}

} else {
System.out.println("not working");
}
}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Not what you want to call here
//f.setVisible(true);
// Probably not the best idea right now
f.setResizable(false);
//f.add(a);
//a.add(p);
// Good idea to add the scollpane to the container
f.add(pane);
p.add(b, BorderLayout.SOUTH);
f.setVisible(true);
}
});
}
}

关于java - 滚动 Pane 不会出现,直到我单击它们并且没有可用的栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683456/

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