Tag: swing

JComboBox弹出菜单没有出现

我在JPanel中有一个JComboBox(它本身嵌套在其他一些JPanel中)。 它包含了一个枚举成员。 我遇到了一个问题,当我单击展开按钮时,弹出菜单没有出现。 这是我到目前为止收集的信息: 1)首次单击展开按钮不执行任何操作。 第二次单击会突出显示该框的内容,但仍不会显示弹出窗口。 2)一旦我点击按钮并给它焦点,上/下击键就可以正确地循环输入。 3)我尝试添加PopupMenuListener来打印事件,事件以无法解释的方式触发: mouse down: popupMenuWillBecomeVisible fires mouse up: nothing mouse down: popupMenuWillBecomeInvisible fires mouse up: nothing 这是我对JComboBox的初始化代码: comboBox = new JComboBox(); comboBox.setPreferredSize(new Dimension(175, 30)); comboBox.setMaximumSize(new Dimension(175, 30)); comboBox.setAlignmentX(0.5f); comboBox.addItem(Enum.Value1); … parentPanel = new JPanel(); parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.X_AXIS)); parentPanel.setMaximumSize(new Dimension(37267, 50)); … add some other stuff to parentPanel … parentPanel.add(comboBox); […]

如何使用JFormattedTextField只允许字母和数字

我有这个代码,无法正确使用MaskFormatter MaskFormatter之外 MaskFormatter formatter = null; try { formatter = new MaskFormatter(“HHHHHHH”); } catch (ParseException e) { e.printStackTrace(); } txtTroll = new JFormattedTextField(formatter); 我需要任何hex字符(0-9,az或AZ)和“H”应该 只给我(0-9,az或AZ),但我错了。 当我输入文字时,只输入大写字母并且输入速度很慢 当我点击txtTroll所有字母都会消失

JPanel没有出现

JPanel没有出现,我把.setVisible但是..它没有出现,它构建但它没有显示我只是这个编程部分的初学者。 package calculatorv3; public class CalculatorForm extends javax.swing.JPanel { private static final long serialVersionUID = 1L; public CalculatorForm() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. […]

Java JPanel鼠标侦听器无法在其组件上运行

这个问题的解决方案是什么? 我在JPanel中添加了一个鼠标监听器,但该面板上充满了覆盖整个JPanel区域的按钮。 示例(伪)代码: Create JPanel Set to GridLayout Add 25 buttons (5×5 grid) Add MouseListener to JPanel 的MouseListener /的MouseMotionListener: onMouseMove { print out X,Y co-ords of mouse } 在我直接进入JPanel容器边缘之前,这些合作伙伴永远不会打印出来,因为按钮会阻挡其余部分。 如何让鼠标监听器在所有面板的组件上工作,而不必将监听器添加到每个组件 – 或者我是否应该将监听器添加到每个组件? 添加信息:我正在尝试为触摸屏系统添加触摸手势(滑动面板会导致操作发生)。 我对自己识别手势非常有信心,但我真的在寻找一种“更好”的方式,而不是为每个组件添加一个监听器副本(这对我来说更糟糕,因为组件正在发生变化)。 我打算尝试将它添加到glassPane而不是……(此刻getRootPane()给我NullPointerException) 编辑:我现在知道我正在尝试从不是根容器的JPanel getRootPane(),这就是为什么我得到一个null 。 我需要在主JFrame上执行此操作。 编辑2:好的,所以我已经完成了(添加玻璃窗格到主JFrame),起初我有一个问题,因为我没有这样做: myGlassPane.setVisible(true); 所以它似乎无法正常工作。 但是,一旦我这样做,我所要做的就是将opaque标志设置为false以使其透明 myGlassPane.setOpaque(false); 所以现在我将X,Y合作打印出按钮和所有内容,但按钮不起作用,因为它们上面有一个面板。

如何在我的JFrame中居中按钮?

我正在尝试制作一个棋盘游戏,我有东部和西部的玩家1标签和玩家2标签。 我无法将3 x 10按钮放在中心位置。 有没有更简单的方法一次制作所有30个按钮? 以下是我的代码: JPanel panel = new JPanel(); JButton button1 = new JButton();JButton button2 = new JButton(); JButton button3 = new JButton();JButton button4 = new JButton(); JButton button5 = new JButton();JButton button6 = new JButton(); JButton button7 = new JButton();JButton button8 = new JButton(); JButton button9 = new JButton();JButton button10 = new […]

在hover时更改JList项目背景颜色

我正在尝试更改JList单元格的背景颜色,但是我不知道该怎么做。 这是我目前拥有的: package cats.youtube.gui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.util.LinkedList; import javax.swing.AbstractListModel; import javax.swing.DefaultListCellRenderer; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ListSelectionModel; import javax.swing.border.EmptyBorder; import cats.youtube.search.YoutubeSearchResult; public class SearchResultsList extends JPanel{ private class Renderer extends DefaultListCellRenderer{ public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean […]

我可以在一个类中使用多个ActionListener吗?

考虑下面过于简化的例子; 其中一些用伪代码编写,以简洁起见。 当我尝试按原样运行时,我收到编译器错误,指出已在我的main方法中找到actionPerformed 。 但是,如果我重命名它,对actionPerformed2说,它不再被ActionListener识别。 我是否需要将foo和foo2方法的侦听器组合到一个ActionListener方法中? 在具有多个按钮对象的单个类中使用多个侦听器时,如何正确区分侦听器? 我刚刚开始玩摆动组件,所以我怀疑我可能不会问正确的问题……但是我可以随时编辑。 🙂 public class foo { declare button1, button2, button3 and panel1 public foo() { show panel1 with button1 and button2; } public foo2() { show panel1 with button3; } public void actionPerformed(ActionEvent e) { Object source1 = e.getSource(); do some stuff when button1 is clicked } public […]

SwingWorker在multilpe面板中更新多个combobox

我有一个小的gui程序,在启动时从Excel文件中读取数据,其中一些数据需要转到相关的combobox。 我知道如何通过为每个combobox使用单独的SwingWorker来做到这一点: public class ExcelReader extends SwingWorker { private final DefaultComboBoxModel model; // Constructor called from a panel that has a combobox public ExcelReader(DefaultComboBoxModel model) { this.model = model; } @Override protected DefaultComboBoxModel doInBackground() throws Exception { ///// code to read data from Excel file ///// publish(someString) used here return model; } @Override protected void […]

如何创建专业定制的Java Swing Gui?

我最近被赋予了完全修改我公司的一个软件Gui外观的任务。 到目前为止,我只做了基本的外观改变。 主要通过一些GridBagConstraints处理将Gui从默认的Metal L&F升级到System L&F。 但这项任务有所不同: 我们有一位平面设计师绘制了所需的设计: 我知道如何创建这种function 。 我可能会使用双JTabbedPanes或只是添加Jpanels。 我的问题是如何将图形与gui对象合并。 为了制作一个看起来像这样的Java Swing应用程序,我应该问设计师给我什么 – 使用圆角标签和标签面板颜色的合并? 当我现在给出的所有内容都是上面的这张图时,我怎样才能让JPanels或JTabbedPanes穿上这件华丽的服装呢?

Java Swing JToolBar

我创建了JToolBar (Java Swing)。 我在包含JToolBar帧上设置了背景图像。 我希望我的JToolBar是透明的,这样保留在框架上的图像应该是可见的。 我正在使用setOpaque(false)但它对我的工具栏没有任何影响..有没有人以前使用过这个? 我的守则如下 import javax.swing.ImageIcon; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class NewJFrame extends javax.swing.JFrame { static NewJFrame dialog = null; /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); jToolBar1.setOpaque(false); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify […]