java - 如何在边框中添加按钮?

标签 java swing user-interface jbutton

我正在编写一个程序,我想在 JTabbedPane 的右上角添加按钮和标签。我使用 MatteBorder,我想在蓝色边框所在的位置添加标签(不是标题)和 1 个按钮。感谢您的帮助。

import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;



public class hotels extends JFrame{

    JButton hotel;
    JLabel image;
     JTabbedPane tabbed,tabbed1;
    JPanel panel;
    JPanel panel1;

    Container pane;
    JPanel panel2;
    JLabel departure;
    JLabel from;
    JLabel to;


    public hotels(){


         panel=new JPanel();
        panel.setBackground(Color.cyan);
        hotel=new JButton();
        hotel.setText("Hotels");

        Font myFont = new Font("Serif", Font.BOLD, 18);


        hotel.setFont(myFont);
        panel.setLayout(null);
        panel.add(hotel);

        hotel.setBounds(50, 80, 100, 40);


        image=new JLabel();

        image.setBounds(50,1,80,80);
        image.setBorder(BorderFactory.createLineBorder(Color.yellow));
        image.setBackground(Color.white);
        image.setIcon(new ImageIcon("2.gif"));
        panel.add(image);

       panel1=new JPanel();
        panel1.setLayout(null);

      tabbed=new JTabbedPane();




         tabbed.setBorder(BorderFactory.createMatteBorder(24, 6, 12, 6, Color.blue));

          Font myFont1 = new Font("Serif", Font.BOLD, 18);
          tabbed.setFont(myFont1);

            tabbed.setBounds(0, 20, 80, 40);

     tabbed.add( "Round Trip",panel1);

     panel2=new JPanel();
     panel2.setLayout(null);
     panel2.setBackground(Color.white);
     departure=new JLabel();
     departure.setText("Departure");

     Font f=new Font("Serif", Font.PLAIN,12);
     departure.setBounds(50,1,80,80);
     departure.setFont(f);
     panel2.add(departure);
       tabbed.add("One Way",panel2);
       from=new JLabel();
       from.setBackground(Color.blue);

       panel1.setBackground(Color.yellow);



        pane=getContentPane();


          pane.add(panel);

   image.addMouseListener(new MouseAdapter()

   {

      public void mouseClicked(MouseEvent e){
           if (e.getSource()==image){

      pane.removeAll();

      pane.add(tabbed);
      pane.revalidate();
      pane.repaint();

           }


       }


   }


   );

    }



    public static void main(String[] args) {

 hotels mw=new hotels();
  mw.setVisible(true);
  mw.setSize(300, 400);


    }



    }

最佳答案

I want to add button and label on top right above JTabbedPane.

您可以通过使用适当的布局嵌套 ContainerJTabbedPane 上方添加 Components。例如,要将 JLabelJButton 放置在 JTabbedPane 的上方和右上角,您可以使用 BoxLayout - 在 mouseClicked 实现中,其中添加了 JTabbedPane:

pane.removeAll();
Box vert = Box.createVerticalBox();
Box top = Box.createHorizontalBox();
top.add(Box.createHorizontalGlue());
top.add(new JLabel("Top Label"));
top.add(new JButton("Top Button"));
vert.add(top);
vert.add(tabbed);
pane.add(vert);
....

您的要求并不明确,因此您可能需要调整背景颜色和边框位置(例如,将边框添加到 vert 而不是 tabbed)以满足您的要求。

关于java - 如何在边框中添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280318/

相关文章:

java - if 条件 : boolean expression variables placing order

java - 混淆 Java Swing 线程中的事件顺序

java - 如何制作 Java 跨平台 GUI 应用程序(Windows、Linux)?我应该使用什么工具?

java - 如何自定义Swing组件(标准化字体)?

winforms - 界面测试工具?

ios - 如何在 iPhone 上显示类似 HTML 的表格数据?

java.sql.SQLIntegrityConstraintViolationException异常

java - 如何在不依赖于位置的回收 View 上设置 OnClickListener

java - 安卓服务器: why it times out incoming connections randomly?

java - 读取图像宽度和高度的非常奇怪的问题(Java)