ява пример добавления кнопки свинг
Primary tabs
вот такой непутёвый код как бы добавит кнопку - чисто картинку , пока никакого функционала
нормальный пример посмотрите здесь = http://fkn.ktu10.com/?q=node/2042
а также - ниже
import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; //import javax.swing.JPanel; import javax.swing.JButton; import java.awt.Dimension; // import javax.swing.*; // импортируем весь свинг, дабы не заморачиваться(мы ведь только учимся)) import java.awt.*; // и весь авт аналогично public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); frame.setPreferredSize(new Dimension(400, 300)); // задаём размер окна frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); // frame.setSize(400, 500); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); // добавляем нашу метку (текст) // теперь давайте добавим кнопку // JButton button = new JButton("Test button"); // button.setMaximumSize(new Dimension(5,5)); // frame.getContentPane().add(BorderLayout.NORTH,button); // button.setPreferredSize(new Dimension(40, 40)); // frame.getContentPane().add(button); JButton button1 = new JButton("Test button"); button1.setMaximumSize(new Dimension(5,5)); JPanel flowNorth = new JPanel(); // defaults to centered FlowLayout flowNorth.add(button1); frame.getContentPane().add(BorderLayout.NORTH, flowNorth); // JPanel panel = new JPanel(new GridLayout(12,23,67,78)); //panel.add(button); ActionListener actionListener = new TestActionListener(); // button.addActionListener(actionListener); // frame.getContentPane().add(button); // далее прописывем некоторые параметры для нашего фрэйма (окна) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } а вот хороший пример добавления кнопки =
import javax.swing.*; import java.awt.Color; public class HelloWorldSwing{ public JPanel createContentPane (){ // We create a bottom JPanel to place everything on. JPanel totalGUI = new JPanel(); totalGUI.setLayout(null); // Creation of a Panel to contain the title labels JPanel titlePanel = new JPanel(); titlePanel.setLayout(null); titlePanel.setLocation(10, 0); titlePanel.setSize(250, 30); totalGUI.add(titlePanel); JLabel redLabel = new JLabel("Red Team"); redLabel.setLocation(0, 0); redLabel.setSize(100, 30); redLabel.setHorizontalAlignment(0); redLabel.setForeground(Color.red); titlePanel.add(redLabel); JLabel blueLabel = new JLabel("Blue Team"); blueLabel.setLocation(120, 0); blueLabel.setSize(100, 30); blueLabel.setHorizontalAlignment(0); blueLabel.setForeground(Color.blue); titlePanel.add(blueLabel); // Creation of a Panel to contain the score labels. JPanel scorePanel = new JPanel(); scorePanel.setLayout(null); scorePanel.setLocation(10, 40); scorePanel.setSize(250, 30); totalGUI.add(scorePanel); JLabel redScore = new JLabel("0"); redScore.setLocation(0, 0); redScore.setSize(100, 30); redScore.setHorizontalAlignment(0); scorePanel.add(redScore); JLabel blueScore = new JLabel("0"); blueScore.setLocation(120, 0); blueScore.setSize(100, 30); blueScore.setHorizontalAlignment(0); scorePanel.add(blueScore); // Creation of a label to contain all the JButtons. JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(null); buttonPanel.setLocation(10, 80); buttonPanel.setSize(250, 70); totalGUI.add(buttonPanel); // We create a button and manipulate it using the syntax we have // used before. JButton redButton = new JButton("Red Score!"); redButton.setLocation(0, 0); redButton.setSize(100, 30); buttonPanel.add(redButton); JButton blueButton = new JButton("Blue Score!"); blueButton.setLocation(120, 0); blueButton.setSize(100, 30); buttonPanel.add(blueButton); JButton resetButton = new JButton("Reset Score"); resetButton.setLocation(0, 40); resetButton.setSize(220, 30); buttonPanel.add(resetButton); totalGUI.setOpaque(true); return totalGUI; } private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] JButton Scores! [=]"); //Create and set up the content pane. HelloWorldSwing demo = new HelloWorldSwing(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 190); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
- vedro-compota's blog
- Log in to post comments
- 6394 reads