gpt4 book ai didi

java - 每当更新发生时,JList 位置都会重置为默认值

转载 作者:行者123 更新时间:2023-11-30 03:12:10 25 4
gpt4 key购买 nike

list 是接受来自 Action1 的输入,这是可行的,但是,每当将新元素添加到列表中时,列表的位置就会移回到默认的 中上 位置。

当调整框架大小时也会发生这种情况,因此作为临时修复,我使用了 frame.setResizes(false) 行,但我不希望它成为永久性的。

我该如何解决这两个问题?

enter image description here

enter image description here

import static java.lang.String.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;

public class lists
{

static String newUrl;
static DefaultListModel<String> model = new DefaultListModel<String>();
static int listXCoord = 650;
static int listYCoord = 10;

public static void createGUI()
{

JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

JPanel panel = new JPanel();
frame.add(panel);

JButton addurl = new JButton("Add URL");
panel.add(addurl);
addurl.addActionListener(new Action1());
JButton remurl = new JButton("Remove URL");
panel.add(remurl);


//model.addElement("one");
//model.addElement("two");
//model.addElement("three");

JList list = new JList<String>(model);
list.setCellRenderer(new DefaultListCellRenderer());
list.setVisible(true);
list.setLocation(listXCoord, listYCoord);
list.setBackground(new Color(186, 203, 250));
//list.setLocation(650, 10);


panel.add(list);
list.setSize(130, 540);
}
static class Action1 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{

newUrl = JOptionPane.showInputDialog("Enter the URL to be Launched");
model.addElement(newUrl);
}
}
public static void main(String[] args)
{

createGUI();

}
}

最佳答案

基本上,您正在与布局管理器(Flowlayout)作斗争并且失败。当您向 JList 添加新元素时,容器层次结构将被重新验证,这会导致布局管理器重新布局其容器的内容

基本的解决方案是使用不同的布局,但是,JFrame 使用 BorderLayout,因此无需将 JList 添加到JPanel,您只需将其添加到框架的 EAST 位置即可

Layout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Lists {

static String newUrl;
static DefaultListModel<String> model = new DefaultListModel<String>();
static int listXCoord = 650;
static int listYCoord = 10;

public static void createGUI() {

JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
frame.add(panel);

JButton addurl = new JButton("Add URL");
panel.add(addurl);
addurl.addActionListener(new Action1());
JButton remurl = new JButton("Remove URL");
panel.add(remurl);

//model.addElement("one");
//model.addElement("two");
//model.addElement("three");
JList list = new JList<String>(model);
list.setCellRenderer(new DefaultListCellRenderer());
list.setVisible(true);
list.setLocation(listXCoord, listYCoord);
list.setBackground(new Color(186, 203, 250));
//list.setLocation(650, 10);

frame.add(new JScrollPane(list), BorderLayout.EAST);

frame.setVisible(true);
}

static class Action1 implements ActionListener {

public void actionPerformed(ActionEvent e) {

newUrl = JOptionPane.showInputDialog("Enter the URL to be Launched");
model.addElement(newUrl);
}
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

createGUI();
}
});

}
}

参见Laying Out Components Within a Container , How to Use BorderLayoutHow to use FlowLayout了解更多详情。

您还应该在所有组件都添加到框架后最后调用 setVisible ,这样可以减少某些组件在您认为应该显示时不显示的可能性。

JList 也将受益于包含在 JScrollPane 中。请参阅How to Use ListsHow to Use Scroll Panes了解更多详情

关于java - 每当更新发生时,JList 位置都会重置为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404068/

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