【问题标题】:Get a JTextFeild to go back to a transparent state获取 JTextField 以返回透明状态
【发布时间】:2016-02-22 05:50:49
【问题描述】:

当我单击按钮时,我试图让JTextField 显示在JButton 之上。我有这个工作,但是当我点击按钮时它仍然可见。我正在使用MouseListener 事件,所以一旦我退出按钮,我希望JTextField 再次变得透明,但它仍然可见。

我的代码:

import java.awt.EventQueue;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;


public class magicalJtextField extends JFrame implements MouseListener{

private JPanel contentPane;
private JTextField textField;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                magicalJtextField frame = new magicalJtextField();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public magicalJtextField() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    textField = new JTextField();
    textField.setBounds(78, 78, 89, 30);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton button = new JButton("");
    //button transparent
//      button.setOpaque(false);
//      button.setContentAreaFilled(false);
//      button.setBorderPainted(false);
    button.setBounds(78, 78, 89, 23);
    button.addMouseListener(this);
    contentPane.add(button);

    textField.setVisible(false);

}
public void mouseEntered(MouseEvent e) 
{
//button.setText("Mouse Entered");
//button.setBackground(Color.CYAN);
//  textField.setVisible(true);
}

public void mouseExited(MouseEvent e) 
{
    textField.setVisible(false);    
}   
public void mouseClicked(MouseEvent e) 
{
    textField.setVisible(true);
}
public void mousePressed(MouseEvent e) 
{
    textField.setVisible(true);
}   
public void mouseReleased(MouseEvent e)
{
    textField.setVisible(true);
}
}

【问题讨论】:

  • Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 尝试拨打repaint
  • 点击退出按钮”是什么意思?对文本字段的关注松散?提交文本字段中的文本?

标签: java swing jbutton jtextfield mouselistener


【解决方案1】:

我建议CardLayout 用于Jbutton-JTextField 魔术(编辑:我实际上是在我发布后才看到 cmets 中的建议,因为它是如此明显并且在答案中)。按下按钮会切换卡片,然后用鼠标退出文本域区域会再次切换。

public class Example extends JPanel {

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.add(new Example());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        });
    }

    public Example() {

        CardLayout cards = new CardLayout(5, 5);
        JPanel panel = new JPanel(cards);

        JButton button = new JButton("");
        JTextField textField = new JTextField(10);

        button.addActionListener(e -> {
            cards.next(panel);
            textField.requestFocusInWindow();
        });

        textField.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseExited(MouseEvent e) {

                cards.next(panel);
            }
        });

        panel.add(button);
        panel.add(textField);

        add(panel);
    }
}

正如Andrew Thompson 所说,不要使用null 布局,也不要指定边界。使用适当的布局管理器为您执行此操作。

【讨论】:

    【解决方案2】:

    使用ActionListener 对按钮点击做出反应:如果您收到点击,则使按钮不可见,而 textField 可见。

    然后将MouseListener 附加到文本字段而不是按钮,并且只实现mouseExited(所有其他为空)。当您收到此事件时,使 textField 不可见并且按钮再次可见。

    【讨论】:

    • “如果您收到点击,请使按钮不可见,而 textField 可见。” 非常适合 CardLayout,如 this answer 所示。
    • "然后附加 MouseListener.." 呃.. 喜欢使用键盘的用户呢?似乎有一个焦点监听器会更好。
    • @AndrewThompson 显然 OP 正在尝试实现一些效果。当然有更有效的方法来做到这一点(例如 CardLayout)。而且图形用户界面不是很好用。但是当你是一个学习者时,尝试这样的事情仍然是有意义的。
    • “但是当你是一个学习者时,尝试这样的事情仍然是有意义的。”对于新手来说,更好的学习是如何使一个可用GUI。 这种废话最好留给手头有太多时间的专家(即便如此,这通常也是个坏主意——只是他们需要“摆脱系统”的东西)。
    猜你喜欢
    • 2018-03-09
    • 2013-07-17
    • 1970-01-01
    • 2014-05-07
    • 2012-05-30
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多