标签: jcomponent

如何获取paint/paintComponent生成的图像?

我有一个快速的问题.如何获取JComponent.paint或paintComponent生成的图像?

我有一个JComponent,我用它作为'工作区',我已经覆盖了paintComponent方法到我自己的方法.问题是我的工作区JComponent也有子项,它们有自己的paintComponent方法.

因此,当Swing渲染我的工作区组件时,它会渲染工作区图形,然后渲染其子图形.

但是,我想获取工作区组件生成的图像(包括工作区图形和子图形).

我怎么做?

我试图通过使用我自己的Graphics自己调用paintComponent/paint-method,但我刚刚返回了一个黑色图像.这是我尝试过的;

public void paintComponent(Graphics g) {

    if (bufferedImage != null) {
        g.drawImage(bufferedImage, 0, 0, this);
    }
    else {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    }
}

public BufferedImage getImage() {

    BufferedImage hello = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = hello.getGraphics();
    paintComponent( g );

    return hello;
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何想法或意见!:)

java swing paint jcomponent

5
推荐指数
1
解决办法
9558
查看次数

如何重新计算JComponent的首选大小?

我知道当我创建一个JComponent的实例时,它有自己的首选大小.现在让我们假设我setPreferredSize手动JComponent的维度为0 x 0.我希望该组件"重置"自己的preferredSize.我怎么做?

java swing jcomponent

5
推荐指数
1
解决办法
3816
查看次数

如何在JTextPane中的组件周围包装文本?

我不理解JTextPane中的包装行为.如果我插入一个短文本,然后是一个JComponent然后再短文本我可以在一行中看到插入的东西,如果框架当然足够大.但是如果文本更长,那么它需要几行,组件总是放在一个新行中.

我已经认识到,在将一个组件插入JTextPane后,它的文本会变长一个字符.因此,如果一个组件被JTextPane视为一个字符,为什么它不像一个字符呢?可能它取决于java版本?我使用Java(TM)SE运行时环境(版本1.7.0-b147)

下面是我的代码(使用shortText/longText实例化变量currentText以重现上述行为):

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");

        String shortText = "one two three four five six seven";
        String longText = "A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a …
Run Code Online (Sandbox Code Playgroud)

java swing insert jtextpane jcomponent

5
推荐指数
1
解决办法
1138
查看次数

从popmenu动作事件中获取右键单击位置

我有一个java程序,在JPanel中右键单击时打开一个弹出菜单.单击任何弹出菜单项时,我想打印在终端中触发弹出菜单的右键单击位置.我该怎么做呢?如何在弹出操作事件中获取右键单击发生位置的位置?

如果弹出菜单位于JComponent中,代码如何更改?

这是程序.

    import java.awt.EventQueue;
    import java.awt.event.*;
    import javax.swing.*;

    public class MenuTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MenuFrame frame = new MenuFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    class MenuFrame extends JFrame
    {
        public MenuFrame()
        {
            setTitle("MenuTest");
            setSize(300, 200);

            Action cutAction = new TestAction("Cut");
            Action copyAction = new TestAction("Copy");
            Action pasteAction = new TestAction("Paste");

            JPopupMenu popup = new JPopupMenu();
            popup.add(cutAction);
            popup.add(copyAction);
            popup.add(pasteAction);

            JPanel panel = new JPanel();
            panel.setComponentPopupMenu(popup);
            add(panel);

            panel.addMouseListener(new MouseAdapter() …
Run Code Online (Sandbox Code Playgroud)

java swing jcomponent coordinates jpopupmenu

5
推荐指数
1
解决办法
3791
查看次数

添加到JPanel时,Java Swing组件的大小不正确

这是我的第一篇帖子,请原谅我,如果我做错了.

我的问题是:

我正在尝试将组件添加到具有Size等定义值的JPanel中.但是当我将它们添加到Panel时,它们绝对没有它们应该具有的大小和位置.例如:

public class Console extends JFrame {
    private JPanel mainPanel = new JPanel();
    private JTextArea textField = new JTextArea();
    private JTextArea textField2 = new JTextArea();

    public Console() {
        this.setSize(500,300);

        this.mainPanel.setSize(this.getWidth(),this.getHeight());

        this.textField.setEditable(false);
        this.textField.setSize(this.mainPanel.getWidth(), 100);
        this.textField.setPreferredSize(new Dimension(this.mainPanel.getWidth(),this.mainPanel.getHeight()));
        this.textField.setLocation(0, 0);
        this.textField.setText("some text");
        this.textField.setVisible(true);

        this.textField2.setSize(this.mainPanel.getWidth(),200);
        this.textField2.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
        this.textField2.setLocation(0,this.mainPanel.getHeight()-this.textField.getHeight());
        this.textField2.setText("blabla");
        this.textField2.setVisible(true);

        this.mainPanel.add(textField);
        this.mainPanel.add(textField2);
        this.mainPanel.setVisible(true);

        this.add(this.mainPanel);
        // I know you should not call setVisible() in the Constructor, just for making Code more simple here.
        this.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序时,两个JTextArea都非常小,位于中间(不是上面设置),而mainPanel是正确的.我试图在代码中的不同位置调用setSize()和setPreferredSize(),但它没有'工作.据我所知,最好使用LayoutManager来做到这一点,但说实话,我不知道如何正确使用它.我在Oracle Doc上检查了它,但如果有人可以为此发布一个干净的解决方案我会很感激,谢谢你提前.

java size swing jcomponent jpanel

5
推荐指数
1
解决办法
1016
查看次数

从JTextPane通过javax.swing.text.Element获取组件?

我使用a JTextPane来显示字符和符号,后者由自定义绘制表示JComponents.例如,文本窗格可能显示如下内容: 在此输入图像描述 文本窗格是用户可编辑的,允许用户通过任何位置的按钮添加更多符号,并替换所选文本.我是通过这种JTextPane.insertComponent()方法做到的.在应用程序的某个时刻,我需要知道文本窗格中当前显示的内容,并且我不仅指输入的文本,还指其中包含的确切组件.

我经历了广泛的麻烦PositionsDocumentListeners管理我的文本窗格的内容,但我一直造成比我解决的问题更多的问题.这就是为什么我最终决定,我的麻烦可能是由于我的设计错误,所以我决定看看,如果我无法通过文本窗格访问我的组件.

通过搜索文档和源代码AbstractDocument以及其他相关类,我找到了界面javax.swing.text.Element.然后我让我的应用程序输出

for(int i = 0; i < textPane.getDocument().getLength(); i++) {
    System.out.println(((StyledDocument) textPane.getDocument()).getCharacterElement(i));
}
Run Code Online (Sandbox Code Playgroud)

这给了我:

LeafElement(内容)0,4

LeafElement(内容)0,4

LeafElement(内容)0,4

LeafElement(内容)0,4

LeafElement(组件)4,5

LeafElement(内容)5,9

LeafElement(内容)5,9

LeafElement(内容)5,9

LeafElement(内容)5,9

LeafElement(组件)9,10

看到LeafElements我所做的事情似乎有关于在哪个位置显示的内容的某种信息Document,我认为必须能够获得该位置的实际内容.在搜索了另外半个小时后如何获得每个元素代表的内容后,我放弃了并决定在这里发布我的问题,希望你们中的一些人可能知道如何实现这个目标!?

我已经看到这个问题,有人试图访问组件textPane.getComponents(),它返回一个组件数组,其中包含实际包含的组件的确切数量JTextPane,但它们都是类型javax.swing.text.ComponentView$Invalidator,这显然对我没用.也许我只是不知道如何从这里正确地继续,因为对我的符号的原始类型的强制转换不起作用.

TL;博士

如何从文本窗格中获取a JComponent,它位于文本内部JTextPane及其位置?

java swing element jtextpane jcomponent

5
推荐指数
1
解决办法
2198
查看次数

尝试 - 在Python中除了给定的时间

我正在使用Marathon(Java桌面应用程序测试工具)来自动化回归测试.Marathon使用Jython,因此我可以使用Java库和Python库.当我的脚本填写某些字段时,会根据我在先前字段中输入的值显示(或不显示)各个字段.我需要跳过那些不存在的字段,原因很明显.当字段被禁用但仍然存在时,这很好,因为我可以使用

    if Component.isEnabled():
        #do something
    else:
        #do something
Run Code Online (Sandbox Code Playgroud)

问题是组件不存在时.在Java中,有没有办法测试组件的存在?例如,Component.exists()适合我的需要,但组件类中没有这样的方法.

我宁愿通过使用if Component.exists():语句来解决我的问题,但我能够使用try来解决它,除了块.但是,这会导致脚本的主要执行时间问题.它会在抛出异常之前尝试查找组件约2或3分钟.我能看到这个问题的唯一方法是,如果有类似的声明try for x seconds,如果找不到组件则继续.有没有办法限制你尝试任何给定声明的时间?

python java jcomponent

5
推荐指数
1
解决办法
619
查看次数

用Java创建一个简单的自定义JComponent?

我想开始为工作中的项目构建自己的自定义JComponent.我在下面有一个简单的例子,它应该只是在屏幕上创建一个球.(我在互联网上发现了大部分内容),但确实提供了一个不错的起点.我的问题是为什么这段代码不能以我的形式显示球?我做错了什么?

那么应该为自定义JComponent提供的所有基本方法是什么?

码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testBall {
    public static void main(String[] args) {
        new testBall();
    }

    public testBall() {
        JPanel testPane = new JPanel();
        testPane.setBackground(Color.white);
        testPane.setLayout(new GridBagLayout());
        testPane.add(new MyBall(30,30,10));

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(testPane);
        frame.pack();
        frame.setSize(500, 300); 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class MyBall extends JComponent
{
    private static final long serialVersionUID = 1L;
    public MyBall() { }
    public MyBall(int x, int y, …
Run Code Online (Sandbox Code Playgroud)

java swing jcomponent paintcomponent preferredsize

5
推荐指数
1
解决办法
1万
查看次数

检查JPanel是否包含JButton

我已经添加了一个按钮JPanel.如果JPanel包含按钮,我想删除按钮.有没有办法检查是否JPanel包含按钮?

java swing jcomponent jpanel jbutton

4
推荐指数
2
解决办法
6415
查看次数

如何垂直居中JTextPane中的文本和JComponent?

目前它看起来如此

在此输入图像描述

怎么做它看起来如此?

在此输入图像描述

以下是我的代码:

    JFrame f = new JFrame();
    JTextPane textPane = new JTextPane();

    JTextField component = new JTextField("      ");
    component.setMaximumSize(component.getPreferredSize());
    textPane.setSelectionStart(textPane.getDocument().getLength());
    textPane.setSelectionEnd(textPane.getDocument().getLength());
    textPane.insertComponent(component);
    try {
        textPane.getDocument().insertString(textPane.getDocument().getLength(), "text",
            new SimpleAttributeSet());
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    f.add(new JScrollPane(textPane));
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

我发现接近这个主题的单个问题:JTextPane插入组件,错误的垂直对齐 但是如何更改对齐没有答案.但根据那里的讨论,它必须是可能的.

java swing jtextpane jcomponent alignment

4
推荐指数
1
解决办法
4908
查看次数