简体   繁体   English

JLabel和JTextArea无法正常工作

[英]JLabel and JTextArea not working

I am making a window that has a large text area and a small text area under it. 我正在制作一个窗口,该窗口下有一个大文本区域和一个小文本区域。 This is what i have so far: 这是我到目前为止所拥有的:

This is the code I have for it: 这是我的代码:

JPanel window=new JPanel(){
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        ImageIcon ii=new ImageIcon("textEffect.png");
        Image i=ii.getImage();
        g.drawImage(i,0,0,null,this);
    }
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,46);
JScrollPane scroll=new JScrollPane(
    dialog,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);

//main method
public static void main(String[] args){
    new Window();
}
//Makes window and starts bot
public Window(){
    super("Pollockoraptor");
    setSize(600,400);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    dialog.setEditable(false);
    dialog.setOpaque(false);
    dialog.setBackground(new Color(0, 0, 0, 0));
    dialog.setLineWrap(true);
    input.addKeyListener(this);
    label.setVerticalTextPosition(SwingConstants.BOTTOM);
    label.setHorizontalTextPosition(SwingConstants.LEFT);
    window.add(scroll);
    window.add(label);
    window.add(input);
    window.setBackground(new Color(97,118,131));
    add(window);
    setVisible(true);
}'

How do I make the larger textarea partially transparent so I can see the background and how do I move "Say:" to be infront of the smaller textarea? 如何使较大的文本区域部分透明,以便可以看到背景,以及如何将“说:”移到较小的文本区域的前面?

JPanel uses a FlowLayout by default. JPanel默认使用FlowLayout You'll want to change the layout manager, personally, I'd recommend something like GridBagLayout , but that's just me. 您可能要亲自更改布局管理器,我建议您使用GridBagLayout东西,但这就是我自己。

See Laying Out Components Within a Container for more details 有关更多详细信息,请参见在容器中布置组件

To make the JTextArea see through, you're going to have to make the JScrollPane and it's JViewPort transparent as well. 为了使JTextArea透明,您将必须使JScrollPane及其JViewPort透明。

Swing only knows how to paint fully opaque or fully transparent components. Swing仅知道如何绘制完全不透明或完全透明的组件。 You can create a translucent effect by making the component transparent and overriding it's paintComponent method and using an AlphaComposite or painting a with a Color which has an alpha value set to something below 255 您可以通过以下方式创建半透明效果:使组件透明并覆盖其paintComponent方法,并使用AlphaComposite或使用alpha值设置为255以下的Color绘画a

For example... 例如...

And general advice... 和一般建议...

  • Key Bindings over KeyListener 通过KeyListener 键绑定
  • Override getPreferredSize of your custom component to get better results when been laid out 覆盖自定义组件的getPreferredSize ,以便在布局时获得更好的结果
  • Use JFrame#pack over setSize , this will calculate the window size based on the needs of the content and take into account the window frame decorations as well setSize使用JFrame#pack ,这将根据内容的需要来计算窗口大小,同时还要考虑到窗口框架的装饰

How do I make the larger textarea partially transparent so I can see the background 如何使较大的文本区域部分透明,以便可以看到背景

Check out Backgrounds With Transparency for problems when using a transparent background and a general purpose solution you can use to you don't need to do custom painting all the time. 在使用透明背景和通用解决方案时,请查看“ 具有透明性的背景”,以解决问题,而无需始终进行自定义绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM