Tag: swing

使Java应用程序对用户不可见

我正在试图找到一种方法使Java应用程序对用户不可见。 基本上只是试图删除这个 < – 图像 如何才能做到这一点? public class TransparentWindow extends JFrame { public TransparentWindow() { initComponents(); } @SuppressWarnings(“unchecked”) private void initComponents() { setExtendedState(Frame.MAXIMIZED_BOTH); setResizable(false); setUndecorated(true); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); setAlwaysOnTop(true); System.setProperty(“sun.java2d.noddraw”, “true”); WindowUtils.setWindowTransparent(this, true); WindowUtils.setWindowAlpha(this, 0.6f); } public static void main(String[] args) { new TransparentWindow().setVisible(true); } }

在java8中重绘()和双缓冲,bug?

我一直在使用Java 8上的Swing中的动画,并且遇到了一些奇怪的行为:在某些其他不相关的组件上调用repaint()后,组件的内容有时会突然变得陈旧。 下面是代码,它为我重现了这种行为: import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; public class Crosshair extends JPanel { private int currentMouseX = 0; private int currentMouseY = 0; public Crosshair() { addMouseMotionListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { currentMouseX = e.getX(); currentMouseY = e.getY(); repaint(); } }); } @Override protected void paintComponent(Graphics g) { […]

paintComponent()正在绘制其他组件

我正在使用基于此答案中的代码的自定义类来绘制形状像讲话泡泡的背景。 每当我调整应用程序窗口的大小足以使组件在顶部或底部突出时,所述组件的轮廓将被绘制在JScrollPane之外的其他组件之上; 在这种情况下是JPanel 。 在左侧图像中,由于组件仍然可见,因此绘制了JScrollPane底部组件的边框; 而在右侧图像中,所提到的组件不再可见,一切看起来都是预期的。 我认为这与我使用JScrollPane来包含组件并因此允许组件在JPanel下滑动这一事实有关。 我该如何防止这种情况? 主要: public class Main { public static void main(String[] args) { JPanel panel = new JPanel(), panelbar = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panelbar.setLayout(new FlowLayout()); JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JFrame frame = new JFrame(“”); frame.setLayout(new BorderLayout()); frame.setSize(200, 223); for (int i = 0; i […]

java中的配置文件

我创建了一个Swing Application-GUI,其中包含TextFields,Labels,CheckBoxes和ComboBoxes等字段。 当用户输入一些信息时,我希望将文本字段,combobox和复选框的详细信息保存到文件中,下次当用户打开此窗口时,我想要保存在文件中的详细信息,即那些用户输入的上一次加载到GUI中。 有人可以帮我这样做吗? 我希望你理解这个问题,如果没有,我会以更详细的方式解释。 非常感谢你提前。

如何将JRadioButton组与模型一起使用

有没有办法将一组JRadioButtons与数据模型相关联,这样就更容易分辨出哪个按钮(如果有的话)被选中了? 在一个理想的世界中,我想将一组N个radiobuttons与一个enum类相关联,该enum类具有NONE值和一个与每个radiobutton相关联的值。

覆盖JButton paintComponent()不起作用

我想绘制自己的JButton版本,所以我重写了paintComponent()方法,并画了一个渐变roundRect。 这可行,但在那之后,我想在它上面绘制Button的String,并且在编译时,我没有收到任何错误消息。 但是在运行时,我只看到了roundRect,gradient,就像我想要的那样(我也可以点击它),但是String是不可见的…… 这是我的代码: import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class JIconButton extends JButton implements MouseListener { private boolean mouseInside; public JIconButton(String file, String text) { super(text, new ImageIcon(file)); setBorder(new LineBorder(Color.LIGHT_GRAY, 0, true)); setContentAreaFilled(false); setFocusPainted(false); addMouseListener(this); setVisible(true); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void […]

如何设置JMenuItem的大小?

正如你所看到的,拥有这些JMenuItem很难看。 菜单项的宽度非常小。 这是代码: JMenu menuOne=new JMenu(“MenuOne”); JMenu menuTwo=new JMenu(“MenuTwo”); JMenu menuThree=new JMenu(“MenuThree”); JMenu menuFour=new JMenu(“MenuFour”); JMenuBar mbar=new JMenuBar(); //add the menu to menubar mbar.add(menuOne); JMenuItem OneItOne=new JMenuItem(“1”); JMenuItem OneItTwo=new JMenuItem(“2″); menuOne.add(OneItOne); menuOne.addSeparator(); menuOne.add(OneItTwo); mbar.add(menuTwo); mbar.add(menuThree); mbar.add(menuFour); setJMenuBar(mbar); 只需在String中添加一些空格即可(例如”1 ” ),但有没有更好的方法来设置JMenuItem的首选长度? 我尝试过OneItTwo.setSize() ,但失败了。

JPopupMenu行为

我在下面准备了一个小测试用例。 我的问题是当我右键单击窗口时。 JPopupMenu出现但如果我点击JWindow菜单外的任何地方都不会消失。 我必须单击窗口上的某个位置来摆脱它,这不是预期的行为。 编辑:在阅读akf的答案后,我切换到JFrame,当帧处于焦点并且弹出菜单显示时,当您单击另一个窗口时它会消失。 但如果窗口没有焦点,你单击某处菜单不会消失。 import javax.swing.*; import java.awt.*; import java.awt.event.*; class test { static class window extends JWindow implements MouseListener, MouseMotionListener{ JPopupMenu popMenu; JPanel panel = new JPanel(); Point location; MouseEvent pressed; public window(){ addMouseListener( this ); addMouseMotionListener( this ); JLabel label = new JLabel(“JWindow”, JLabel.CENTER); initPopMenu(); add(label); setVisible(true); setAlwaysOnTop(true); setLocationRelativeTo(null); pack(); } […]

从jButton获取文本值

所以我需要简单地检查点击按钮的文本是”X”还是”O” (制作tic tac toe)此代码不起作用: if (jButton1.getText()==”X”) 但是,以下代码确实有效: String jButText = jButton1.getText(); if (jButText==”X”) 为什么第二个代码不能在第二个代码工作时起作用? 是否需要更像if( jButton1.getText().toString==”X” )? 顺便说一句,我不认为toString存在于Java中。 这在Visual Basic中有点相同,这是我通常用来创建GUI的东西。

在JScrollPane中更顺畅地滚动

使用JScrollPane的滚动条箭头滚动(或通过设置箭头键的键绑定)将视口移动一个增量,暂停,然后平滑滚动。 我遇到的大多数滚动条表现相同; 有轻微的动作,暂停,然后更快的连续滚动。 有没有办法避免暂停,以便从头到尾滚动顺畅?