简体   繁体   English

在Java中创建三角形按钮

[英]Creating Triangle button In Java

I am trying to create a triangle button. 我正在尝试创建一个三角形按钮。

I don't know how to do it so i'm gonna need for help in creating and please explane to me how to create it! 我不知道怎么做,所以我需要帮助创建,请向我解释如何创建它!

Here's what I'm trying to achieve: 这是我想要实现的目标:

在此输入图像描述

Any ideas? 有任何想法吗?

According to this , It looks like you just have to subclass JButton and override the paintBorder() and contains() methods 这个 ,它看起来像你只需要继承JButton,并覆盖paintBorder()contains()方法

I create this not so quick and dirty example. 我创建这个不是那么快又脏的例子。 I thought it would be faster but it took me about 15 minutes to come with this. 我觉得它会更快但是我花了大约15分钟就来了。

一个三角形的jbutton

Visually looks flat, because I use the same border and paint the same always, but you might want to provide different representations for onmouse over, click, enable, disable, etc. etc. 视觉上看起来很平坦,因为我总是使用相同的边框并且绘制相同的边框,但是您可能希望为onmouse提供不同的表示,包括,单击,启用,禁用等。

If you run this code, you may see only "Click" text is executed when you actually click inside the triangle: 如果您运行此代码,您可能会看到在实际单击三角形内部时仅执行“单击”文本:

import java.awt.Polygon;
import java.awt.Shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class TriangleButton extends JButton {
    private Shape triangle = createTriangle();

    public void paintBorder( Graphics g ) {
        ((Graphics2D)g).draw(triangle);
    }
    public void paintComponent( Graphics g ) {
        ((Graphics2D)g).fill(triangle);
    }
    public Dimension getPreferredSize() {
        return new Dimension(200,100);
    }
    public boolean contains(int x, int y) {
        return triangle.contains(x, y);
    }

    private Shape createTriangle() {
        Polygon p = new Polygon();
        p.addPoint( 0   , 100 );
        p.addPoint( 100 , 0   );
        p.addPoint( 200 ,100  );
        return p;
    }
}

public class A {

    public static void main( String ... args ) {
        JFrame frame = new JFrame();
        final JButton b =  new TriangleButton();
        b.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Click!");
            }
        });
        frame.add( new JPanel(){{add(b);}} );
        frame.setVisible(true);

    }
}

You probably just want to extend the JButton and override the .paint(Graphics g) method (extending JComponent is semantically incorrect and may cause trouble with other frameworks since this is a button). 您可能只想扩展JButton并覆盖.paint(Graphics g)方法(扩展JComponent在语义上是不正确的,并且可能会导致其他框架出现问题,因为这一个按钮)。 paint is where the code that "paints" the button onto the screen goes. paint是将按钮“绘制”到屏幕上的代码所在的位置。 If you add custom code to draw the button they way you want it, it will appear differently on the screen. 如果您添加自定义代码以按照您希望的方式绘制按钮,它将在屏幕上以不同的方式显示。 You'll probably want to implement java.awt.event.MouseListener for your button so that you can have different effects for your button when the user hovers over it or clicks on it. 您可能希望为按钮实现java.awt.event.MouseListener ,以便在用户将鼠标悬停在按钮上或单击它时,可以为按钮提供不同的效果。

From another answer, you'll want to override contains(int x, int y) so that the clickable area reflects the actual shape of your button. 从另一个答案中,您将要覆盖contains(int x, int y)以便可点击区域反映按钮的实际形状。

But this isn't a quick thing you want to do and you can just grab something readymade from Java, you'll have to make it yourself and it's quite involved but very doable. 但这不是你想要做的快速事情,你可以抓住现成的Java制作,你必须自己制作并且它非常复杂但非常可行。

It is harder to create a custom made GUI button using swingGUI. 使用swingGUI创建自定义GUI按钮更加困难。 So be it simple and making a triangle on a button in netbeans IDE 简而言之,在netbeans IDE中的按钮上创建一个三角形

public class TriangleButton extends javax.swing.JFrame {


public TriangleButton() {
    initComponents();
}

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setIcon(new javax.swing.ImageIcon("triangle.png")); 
    jButton1.setText("text1");
    jButton1.setActionCommand("hii");
    jButton1.setBorder(null);
    jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    jButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
    jButton1.setPressedIcon(new javax.swing.ImageIcon("triangle.png")); 
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(104, 104, 104)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(90, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(52, 52, 52)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(93, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.println("Hiiiiii");
}                                        


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TriangleButton().setVisible(true);
        }
    });
}


private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;


![http://i.stack.imgur.com/KUPWQ.jpg][1]}

There is no standard component that i know of that does this, You will need to create your own. 我知道没有标准组件可以做到这一点,你需要创建自己的组件。

Extend one that is similar or just extend a jpanel. 扩展一个类似的或只是扩展jpanel。

http://www.programmersheaven.com/mb/java/247058/247058/draw-a-triangle/ gives some code for drawing a triangle. http://www.programmersheaven.com/mb/java/247058/247058/draw-a-triangle/给出了绘制三角形的一些代码。

To make it more "button" like you will need a listener. 要使它更像“按钮”,你需要一个听众。

Assuming we're talking about Swing, your best bet would be to take the source code for JComponent , and modify the _ paintImmediately method to draw a triangle instead of a rectangle. 假设我们正在谈论Swing,你最好的选择是获取JComponent源代码 ,并修改_ paintImmediately方法来绘制一个三角形而不是一个矩形。

It would probably be easier to create your own Java GUI library. 创建自己的Java GUI库可能会更容易。

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

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