.drawLine 未绘制到 JTabbedPane (Java) 上

发布于 2024-10-03 10:06:23 字数 3967 浏览 7 评论 0原文

有人告诉我一种在 Jframe 上绘画的方法,它在示例中工作得很好,但现在我有一些选项卡,但它无法在它们上绘画。

我需要 Paint/drawLine 在我的选项卡式示例中工作:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener;
import java.io.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;


public class GUI extends JTabbedPane implements ActionListener
{
    static JFrame aWindow = new JFrame("Project");

    JTabbedPane myTabs = new JTabbedPane();
    
    JPanel  loginMainPanel = new JPanel();
    JPanel  displayMainPanel = new JPanel();
    JPanel  editMainPanel = new JPanel();

    JTextField myText1 = new JTextField("");
    JTextField myText2 = new JTextField("");
    JTextField myText3 = new JTextField("");
    
    JLabel loginLabel = new JLabel("Username:");

    JTextField loginField = new JTextField();
    JLabel loginLabel2 = new JLabel("Password:");
    JPasswordField loginPass = new JPasswordField();

    JButton displayButton = new JButton("Load Data");
    JButton loginButton = new JButton("Login");

    JLabel editLabel = new JLabel("Write:");
    JTextArea editArea = new JTextArea();

    public GUI()
    {
        Toolkit theKit = aWindow.getToolkit();
        Dimension wndSize = theKit.getScreenSize();  
        
        aWindow.setBounds(wndSize.width/3, wndSize.height/3, 250, 250); 
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        GridLayout grid = new GridLayout(1,1);
        Container content = aWindow.getContentPane();
        content.setLayout(grid);
        
        createLoginPanel();
        createDisplayPanel();
        createEditPanel();

        myTabs.addTab("Login", loginMainPanel);
        myTabs.addTab("Main Menu", displayMainPanel);
        myTabs.addTab("Setting", editMainPanel);

        myTabs.setSelectedIndex(0);
        myTabs.setEnabledAt(1,false);
        myTabs.setEnabledAt(2,false);
        
        content.add(myTabs);
        aWindow.setVisible(true);
    }
    
    public void createLoginPanel()
    {
        loginMainPanel.setLayout(null);
        loginLabel.setBounds(10, 15, 150, 20);
        loginMainPanel.add(loginLabel);
        loginField.setBounds(10, 35, 150, 20);
        loginMainPanel.add(loginField);
        loginLabel2.setBounds(10, 60, 150, 20);
        loginMainPanel.add(loginLabel2);
        loginPass.setBounds(10, 80, 150, 20);
        loginMainPanel.add(loginPass);
        loginButton.addActionListener(this);
        loginButton.setBounds(50, 110, 80, 20);
        loginMainPanel.add(loginButton);
    }
    
    public void createDisplayPanel()
    {
        displayMainPanel.setLayout(null);
        displayButton.addActionListener(this);
        displayButton.setBounds(50, 80, 150, 20);
        displayMainPanel.add(displayButton);
        myText1.setBounds(50, 170, 200, 30);
        myText2.setBounds(50, 140, 200, 30);
        myText3.setBounds(50, 110, 200, 30);
        displayMainPanel.add(myText1);
        displayMainPanel.add(myText2);
        displayMainPanel.add(myText3);
    }
    
    public void createEditPanel()
    {
        editMainPanel.setLayout(null);      
        editLabel.setBounds(10, 15, 150, 20);
        editMainPanel.add(editLabel);
        editArea.setBounds(10, 65, 150, 50);
        editMainPanel.add(editArea);        
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == loginButton)
        {
            //myTabs.setSelectedIndex(1);
        }
    }
 
    public void paint(Graphics g) {
        super.paint(g);
        int locX = 0;
        int locY = 0;
    int destX = 210;
    int destY = 210;
    g.setColor(Color.red);
        // draw a line (there is now drawPoint..)
        g.drawLine(locX, locY, destX, destY); 
    }


    public static void main(String[] args)
    {
        GUI tw1 = new GUI();

    }
}

如何找到解决方案,以便它将在选项卡上绘制该行(loginMainPanel)?

Someone told me a way to paint onto a Jframe and it worked fine in the example, but now I have tabs it doesn't paint onto them.

I need the paint/drawLine to work in my Tabbed example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener;
import java.io.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;


public class GUI extends JTabbedPane implements ActionListener
{
    static JFrame aWindow = new JFrame("Project");

    JTabbedPane myTabs = new JTabbedPane();
    
    JPanel  loginMainPanel = new JPanel();
    JPanel  displayMainPanel = new JPanel();
    JPanel  editMainPanel = new JPanel();

    JTextField myText1 = new JTextField("");
    JTextField myText2 = new JTextField("");
    JTextField myText3 = new JTextField("");
    
    JLabel loginLabel = new JLabel("Username:");

    JTextField loginField = new JTextField();
    JLabel loginLabel2 = new JLabel("Password:");
    JPasswordField loginPass = new JPasswordField();

    JButton displayButton = new JButton("Load Data");
    JButton loginButton = new JButton("Login");

    JLabel editLabel = new JLabel("Write:");
    JTextArea editArea = new JTextArea();

    public GUI()
    {
        Toolkit theKit = aWindow.getToolkit();
        Dimension wndSize = theKit.getScreenSize();  
        
        aWindow.setBounds(wndSize.width/3, wndSize.height/3, 250, 250); 
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        GridLayout grid = new GridLayout(1,1);
        Container content = aWindow.getContentPane();
        content.setLayout(grid);
        
        createLoginPanel();
        createDisplayPanel();
        createEditPanel();

        myTabs.addTab("Login", loginMainPanel);
        myTabs.addTab("Main Menu", displayMainPanel);
        myTabs.addTab("Setting", editMainPanel);

        myTabs.setSelectedIndex(0);
        myTabs.setEnabledAt(1,false);
        myTabs.setEnabledAt(2,false);
        
        content.add(myTabs);
        aWindow.setVisible(true);
    }
    
    public void createLoginPanel()
    {
        loginMainPanel.setLayout(null);
        loginLabel.setBounds(10, 15, 150, 20);
        loginMainPanel.add(loginLabel);
        loginField.setBounds(10, 35, 150, 20);
        loginMainPanel.add(loginField);
        loginLabel2.setBounds(10, 60, 150, 20);
        loginMainPanel.add(loginLabel2);
        loginPass.setBounds(10, 80, 150, 20);
        loginMainPanel.add(loginPass);
        loginButton.addActionListener(this);
        loginButton.setBounds(50, 110, 80, 20);
        loginMainPanel.add(loginButton);
    }
    
    public void createDisplayPanel()
    {
        displayMainPanel.setLayout(null);
        displayButton.addActionListener(this);
        displayButton.setBounds(50, 80, 150, 20);
        displayMainPanel.add(displayButton);
        myText1.setBounds(50, 170, 200, 30);
        myText2.setBounds(50, 140, 200, 30);
        myText3.setBounds(50, 110, 200, 30);
        displayMainPanel.add(myText1);
        displayMainPanel.add(myText2);
        displayMainPanel.add(myText3);
    }
    
    public void createEditPanel()
    {
        editMainPanel.setLayout(null);      
        editLabel.setBounds(10, 15, 150, 20);
        editMainPanel.add(editLabel);
        editArea.setBounds(10, 65, 150, 50);
        editMainPanel.add(editArea);        
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == loginButton)
        {
            //myTabs.setSelectedIndex(1);
        }
    }
 
    public void paint(Graphics g) {
        super.paint(g);
        int locX = 0;
        int locY = 0;
    int destX = 210;
    int destY = 210;
    g.setColor(Color.red);
        // draw a line (there is now drawPoint..)
        g.drawLine(locX, locY, destX, destY); 
    }


    public static void main(String[] args)
    {
        GUI tw1 = new GUI();

    }
}

How can I find a solution so it will paint that line on the tab (loginMainPanel)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-10-10 10:06:23

如果您想在 JPanel 上自定义绘图,您应该创建一个扩展 JPanel 的自定义类:

class CustomPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(x1, y1, x2, y2);
    }
}

然后:

JPanel  loginMainPanel = new JPanel();

将变成:

JPanel  loginMainPanel = new CustomPanel();

If you want custom drawing on a JPanel, you should create a custom class that extends JPanel:

class CustomPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(x1, y1, x2, y2);
    }
}

Then:

JPanel  loginMainPanel = new JPanel();

Woudl become:

JPanel  loginMainPanel = new CustomPanel();
ぺ禁宫浮华殁 2024-10-10 10:06:23

对于臃肿的代码造成的模糊表示抱歉

是的,这就是人们无法解决问题的原因,因为代码太臃肿了,你看不到自己在做什么。

如果您希望我们帮助您解决问题,那么您需要发布 SSCCE

有人告诉我一种在 Jframe 上绘画的方法

嗯,这是错误的,一般来说你不应该重写 Paint() 方法,除非你有特定的原因。

另外,您的整个程序是错误的,因为您正在扩展 JTabbedPane。您绝对不应该这样做来创建 GUI。

您的 Paint() 方法永远不会被调用,因为您从未在任何地方使用该类。看看你的代码。对于您的类变量,您创建一个新的 JTabbedPane。然后在构造函数中将所有这些组件添加到框架中并使框架可见。

您需要查看 Swing 教程 并遵循其中的一些内容此处的示例提供了创建简单 GUI 的更好方法。

我不明白你想通过在选项卡式窗格上画一条线来做什么。每次单击选项卡时,选项卡式窗格都会显示不同的面板。您到底希望该线出现在哪里?

另外,您应该学习如何使用布局管理器。使用空布局会导致不必要的问题。

在您发布 SSCCE 之前,我无法提供太多帮助。

Sorry for the blurge of bloated code

Yes, well that is why people can't solve problems, because the code is so bloated you can't see what you are doing.

If you want us to help you problem solve then you need to post a SSCCE

Someone told me a way to paint onto a Jframe

Well, that is wrong, in general you should not be overrding the paint() method, unless you have a specific reason.

Also, your whole program is wrong because you are extending JTabbedPane. You should never do this to create a GUI.

Your paint() method is never invoked because you never use that class anywhere. Look at your code. For your class variables you create a new JTabbedPane. Then in the constructor you add all these components to the frame and make the frame visible.

You need to take a look at the Swing tutorial and follow some of the example there for a better way to create a simple GUI.

I don't understand what you are trying to do by drawing a line on the tabbed pane. A tabbed pane displays different panels every time you click on a tab. Where exactly do you want the line to appear?

Also, you should learn how to use layout managers. Using a null layout will cause unnecessary problems.

Until you post a SSCCE, I can't be of much help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文