Tag: swing

Java JTable更新行

我正在创建一个这样的JTable: String[] colName = new String[] { “ID#”, “Country”, “Name”, “Page titel”, “Page URL”, “Time” }; Object[][] products = new Object[][] { { “123”, “USA”, “Bill”, “Start”, “http://www.url.com”, “00:04:23” }, { “55”, “USA”, “Bill”, “Start”, “http://www.url.com”, “00:04:23” } }; dtm = new DefaultTableModel(products, colName); table = new JTable(dtm); 我怎么能按ID更新行? 我想更新ID等于55的整行。 编辑:我知道如何通过行ID检测,但我如何实际更新单元格? public void removeVisitorFromTable(String visitorID) […]

FEST:在执行任何操作之前等待GUI加载

@Before public void setUp() { Robot robot = BasicRobot.robotWithCurrentAwtHierarchy(); ApplicationLauncher.application(“myApp”).start(); Pause.pause(5, TimeUnit.SECONDS); frame = WindowFinder.findFrame(“frame0”).using(robot); JTableFixture table = frame.table(new GenericTypeMatcher(JTable.class) { @Override protected boolean isMatching(JTable table) { return (table instanceof myTreeTable); } }); } 这段代码效果很好。 如果我们删除5秒暂停,则找不到表,因为应用程序加载它需要几秒钟。 我想知道是否有更清洁的方法。 我在ApplicationLauncher之后尝试使用robot.waitForIdle()(我猜一旦EDT为空,一切都已加载),但它无法正常工作。 我知道暂停可以使用某些条件作为何时停止的事件,但我不明白如何编写它,因为JavaDoc和官方文档很差。 Pause.pause(WaitForComponentToShowCondition.untilIsShowing(frame.component())):我需要一个组件,如果我通过包装器框架它不起作用。 我无法通过这张桌子,因为这正是我正在等待的。 我明白我应该使用ComponentFoundCondition,但我不明白! 我厌倦了: ComponentMatcher matcher = new GenericTypeMatcher(JTable.class) { @Override protected boolean isMatching(JTable table) { […]

智能垂直流量布局

我在JPanel布局(Swing)中寻找以下行为:基本上它会以垂直方式排列组件,一个相互吼叫。 当组件无法垂直放入容器中时,它应该在新行中添加下一个组件。 这将继续动态,根据需要添加新行。 添加3个标签后,看起来会像这样: +————————–+ | label1 | | label2 | | label3 | +————————–+ 添加:2个以上的标签: +————————–+ | label1 label4 | | label2 label5 | | label3 | +————————–+ 最后,添加2个标签后,它看起来像这样: +————————–+ | label1 label4 label7 | | label2 label5 | | label3 label6 | +————————–+ 使用当前布局之一可以实现此行为吗? 我应该自己创建吗? 你会如何解决这个问题?

setPreferredSize的副作用是什么?

我有包含多个面板的窗口。 我无法访问窗口代码。 (我只能修改面板的代码。) 我从面板中删除了一些组件。 窗户缩小了它的尺寸。 但窗口太小,无法正确显示所有内容。 我添加了行setPreferredSize(getPreferredSize()); 。 现在窗口大小合适。 setPreferredSize的副作用是什么? 编辑:使用BorderLayout。 哪个应该忽略getXXXSize()。 我的小组在CENTER。 不适合屏幕的面板位于NORTH。

在jar文件中加载图像

我正在尝试从可执行的JAR文件加载图像。 我已经按照这里的信息,然后是这里的信息。 这是检索图像的function: public static ImageIcon loadImage(String fileName, Object o) { BufferedImage buff = null; try { buff = ImageIO.read(o.getClass().getResource(fileName)); // Also tried getResourceAsStream } catch (IOException e) { e.printStackTrace(); return null; } if (buff == null) { System.out.println(“Image Null”); return null; } return new ImageIcon(buff); } 这就是它被称为的方式: logo = FileConverter.loadImage(“/pictures/Logo1.png”, this); JFrame.setIconImage(logo.getImage()); 这是一个简单的对象。 我也没有得到NullPointerException,除非它被UI掩盖。 […]

SwingWorker,未调用done()方法

这只是SwingWorker的一个实现: class GuiWorker extends SwingWorker { private JFrame frame = new JFrame(); private JDialog dialog = new JDialog(frame, “Loadin data”, true); private JProgressBar progressBar = new JProgressBar(); private Statistics st = new Statistics(); public GuiWorker(GraphEditor editor, Statistics st) { this.st = st; Window mainWindow = SwingUtilities.windowForComponent(editor .getGraphComponent().getParent()); dialog.setSize(400, 200); int x = mainWindow.getX() + (mainWindow.getWidth() […]

使用JFileChooser将图像加载到JFrame中

我正在尝试编写一个代码,将使用JFileChooser选择的图像显示到另一个JFrame中。我尝试了下面的代码,但只得到以下错误。 Exception in thread “main” java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:228) at power.(fCGUI.java:53) at fCGUI.main(fCGUI.java:11) 这是代码: import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class fCGUI { public static void main(String []args) { power p=new power(); p.setVisible(true); } } class power extends JFrame { JFileChooser chooser; BufferedImage img; JButton button,button2; JFrame […]

如何消除动画期间出现的闪烁?

我正在通过在JApplet中制作一个小游戏来学习Java。 我的精灵动画有点问题。 这是代码: this.sprite.setBounds(0,0,20,17); this.sprite.setIcon(this.rangerDown); for(int i = 0; i< 16;i++) { this.sprite.setBounds(this.sprite.getX(), this.sprite.getY()+1, 20, 17); this.sprite.update(this.sprite.getGraphics()); try{ Thread.currentThread().sleep(100); }catch(InterruptedException e){ } } 它在动画期间留下了一些闪烁。 一旦动画结束,闪烁就消失了,但它有点难看……我想我错过了一些步骤。 我使用这种方法是因为它现在提供了更好的结果,但是如果可能的话我想保持不使用AWT,而是使用Swing。 任何想法如何摆脱闪烁? 谢谢阅读。 Screenshoot (无法发布图片,抱歉)。

JComboBox搜索列表

我想制作一个可以搜索内容的JComboBox 。 我尝试过AutoCompleteDecorator, GlazedLists, SwingLabs, JIDE, Laf-Widget ,但是所有的都无法通过第二个关键字进行搜索。 例如,在此代码中,可能按第一个字母搜索,此内容仅包含一个单词。 this.comboBox = new JComboBox(new Object[] { “Ester”, “Jordi”, “Jordina”, “Jorge”, “Sergi” }); AutoCompleteDecorator.decorate(this.comboBox); 如果JComboBox内容包含2或3个单词,例如:“Ester Jordi”或“Jorge Sergi”,在这种情况下,如果我输入“Sergi”, JComboBox不会显示任何内容,因为它可以通过第一个单词识别。 我想问一下有没有办法解决这个问题?

JPanel上的圆角边框

我在这里得到的是一个带有圆形边框的JPanel,设置如下: panel.setBorder(new LineBorder(PanelColor, 5, true)); 问题是,在每个角落中,有2条对角不透明线从外角到内角,即边界的长度。 无论边境背后是什么,都可以看出来。 有没有办法摆脱这些线条,使整个边框一个纯色? 谢谢! 我附上一张照片,但我没有足够的代表这样做:P