按钮大小和位置

发布于 2024-11-08 07:49:46 字数 2264 浏览 2 评论 0原文

嗨,我想把按钮放在南位置!我怎样才能做到这一点?这是我的代码:

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;

public class TableDemo extends JPanel {
    private static Icon leftButtonIcon;
    private boolean DEBUG = false;
     // added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();

    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

       JButton button=new JButton("Buy it");
       button.setSize(30,60);
        button.add(button);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
         //create a button

    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

     public Object getValueAt(int row, int col)
        {
            return rosterList.get(row)[col];

        }


    }


    private static void createAndShowGUI() {
        //Create and set up the window.

        JFrame frame = new JFrame("TableDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

Hi I want to put my button at the South position! How can I do that? Here is my code:

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;

public class TableDemo extends JPanel {
    private static Icon leftButtonIcon;
    private boolean DEBUG = false;
     // added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();

    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

       JButton button=new JButton("Buy it");
       button.setSize(30,60);
        button.add(button);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
         //create a button

    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Κωδικός", "Ποσότητα", "Τιμή", "Περιγραφή", "Μέγεθος", "Ράτσα"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

     public Object getValueAt(int row, int col)
        {
            return rosterList.get(row)[col];

        }


    }


    private static void createAndShowGUI() {
        //Create and set up the window.

        JFrame frame = new JFrame("TableDemo");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

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

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

发布评论

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

评论(4

暗藏城府 2024-11-15 07:49:48

试试这个:

JFrame frame = new JFrame();
frame.add(yourbutton,BORDERLAYOUT.SOUTH)

为了给按钮提供位置和大小,请执行以下操作

yourbutton.setBounds(10,15,120,200) --> (x,y,height,width) , x , y set possition

try this:

JFrame frame = new JFrame();
frame.add(yourbutton,BORDERLAYOUT.SOUTH)

and for give position and size to button,do this

yourbutton.setBounds(10,15,120,200) --> (x,y,height,width) , x , y set possition
若沐 2024-11-15 07:49:47

像这样的东西吗?

在此处输入图像描述

查看代码中的注释。

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;

public class TableDemo extends JPanel {    
    private static Icon leftButtonIcon;
    private boolean DEBUG = false;
     // added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();

    public TableDemo() {
        super(new BorderLayout(3,3));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

       JButton button=new JButton("Buy it");
       // Rarely has the intended effect.
       // also best not to presume we can guess the size
       // a component needs to be.
       //button.setSize(30,60);
       // cannot add a button to itself!
        //button.add(button);
        JPanel buttonCenter = new JPanel( new FlowLayout(FlowLayout.CENTER) );
        // allow the button to be centered in buttonCenter,
        // rather than stretch across the width of the SOUTH
        // of the TableDemo
        buttonCenter.add(button);
        add(buttonCenter, BorderLayout.SOUTH);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane, BorderLayout.CENTER);
         //create a button

        // add a nice border
        setBorder(new EmptyBorder(5,5,5,5));
    }

    class MyTableModel extends AbstractTableModel {
        // apologies about the column names
        private String[] columnNames = { "??d????", "??s?t?ta", "??µ?", "?e????af?", "???e???", "??tsa"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

     public Object getValueAt(int row, int col)
        {
            return rosterList.get(row)[col];

        }
    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        // JPanels are opaque by default!
        //newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Should be done on the EDT.
        // Left as an exercise for the reader.
        TableDemo.createAndShowGUI();
    }
}

Something like this?

enter image description here

See the comments in the code.

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.*;
import java.util.*;
import javax.swing.JButton;

public class TableDemo extends JPanel {    
    private static Icon leftButtonIcon;
    private boolean DEBUG = false;
     // added static infront becuase got non static referencing error
static List<String[]> rosterList = new ArrayList<String[]>();

    public TableDemo() {
        super(new BorderLayout(3,3));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

       JButton button=new JButton("Buy it");
       // Rarely has the intended effect.
       // also best not to presume we can guess the size
       // a component needs to be.
       //button.setSize(30,60);
       // cannot add a button to itself!
        //button.add(button);
        JPanel buttonCenter = new JPanel( new FlowLayout(FlowLayout.CENTER) );
        // allow the button to be centered in buttonCenter,
        // rather than stretch across the width of the SOUTH
        // of the TableDemo
        buttonCenter.add(button);
        add(buttonCenter, BorderLayout.SOUTH);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane, BorderLayout.CENTER);
         //create a button

        // add a nice border
        setBorder(new EmptyBorder(5,5,5,5));
    }

    class MyTableModel extends AbstractTableModel {
        // apologies about the column names
        private String[] columnNames = { "??d????", "??s?t?ta", "??µ?", "?e????af?", "???e???", "??tsa"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }

     public Object getValueAt(int row, int col)
        {
            return rosterList.get(row)[col];

        }
    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        // JPanels are opaque by default!
        //newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Should be done on the EDT.
        // Left as an exercise for the reader.
        TableDemo.createAndShowGUI();
    }
}
怪异←思 2024-11-15 07:49:47

您将按钮添加到自身,并且应该使用 BorderLayout< /a>,如果您想将组件放置在中心/北/南/等方式:

setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);

You add the button to itself, and you should use BorderLayout, if you want to place the components in Center / North / South / etc. manners:

setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
揪着可爱 2024-11-15 07:49:47

“嗨,我想把按钮放在南边位置!我该怎么做?”

如果你想在 BorderLayout 位置放置一些东西,那么让容器使用 ... BorderLayout 是有意义的,这不是吗?

但说真的,您最近在这个论坛上提出的大多数问题都表明您在阅读教程之前先来这里。您已经多次收到这些链接,所以请帮自己一个忙,正确学习 Swing——学习布局教程和其他教程,这样您就会避免很多悲伤和猜测。

"Hi i want to put my button at the South posotion! how can i?"

If you want to place something in a BorderLayout location, it would make sense to have the container use a ... BorderLayout, n'est-ce pas?

But seriously, most of your recent questions in this forum suggest you're coming here before reading the tutorials. You've already been given the links several times, so please do yourself a favor and learn Swing right -- study the layout tutorials and other tutorials and you will save yourself a lot of grief and guessing.

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