📜  JButton - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:15.563000             🧑  作者: Mango

代码示例2
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Jbutton {

    public static void main(String[] args) {

        JFrame frame = new JFrame("GRProgram");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int width = 400;                                        // Window Value Width 
        int height = 300;                                        // Window Value Height                                
        frame.setSize(width,height);                            // Window Dimension with Variables 
        frame.setLayout(null);                                    //better Layout
               
        JButton button = new JButton();
        String bText = "My Button in a long version";            // String value for Button
        button.setText(bText);                                    // set String for Button Text
        String tText = "Tool Tip text";                            // String value for ToolTip on Button
        button.setToolTipText(tText);                            // set String for Button ToolTipText
        Font schriftart=new Font("Arial",Font.BOLD,18);            // Define a Font, mayby Bold and size
        button.setFont(schriftart);                                // Set Font on Botton
        Dimension size = button.getPreferredSize();                // Get prefered Size for Button on Text Size 
        button.setBounds(width/5, height/5, size.width, size.height);    // Set Button Start Point (Left upper Corner) and Size on text Size
        
        frame.add(button);
        frame.setVisible(true);
        
    }

}