Cómo obtener la alineación adecuada del borde del título sobre el Jtable

Estoy tratando de construir una pequeña aplicación en java swings. Empecé a usar este enlace: Crear una cuadrícula en Java.

Todo era bueno. Terminé esa tarea, pero queda algo que estoy tratando de eliminar el borde del título adicional. Traté de reducir el borde del borde del título y aún así no afecta. Cualquiera con una buena sugerencia intenta compartir conmigo.

    package swings.application.framework;

    import java.awt.*;
    import java.awt.event.ActionEvent

;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Inf3rNix
 */
public class KidsProblemTable {
 private static JButton okbtn;

           private static     JFrame frame;
    public static void main(String[] args) {
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();

         frame = new JFrame("Kids Table");


                Border border = LineBorder.createGrayLineBorder();
                frame.setLayout(null);

                frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
                JLabel Heading = new JLabel("My Result");
                Heading.setFont(new Font("Gabriola", Font.ITALIC, 56));
                Heading.setForeground(Color.BLUE);
                Heading.setBounds(550, -60, 300, 200);

                JLabel Heading2 = new JLabel("Nice Job! Here are your results...");
                Heading2.setFont(new Font("Buxton Sketch", Font.ITALIC, 26));
                Heading2.setForeground(Color.BLUE);
                Heading2.setBounds(500, 10, 600, 200);

                JLabel Heading3 = new JLabel();

                String str = "You got 5 correct answer out of 7 question";
                Heading3.setText("<html><a href=' ' style='color:rgb(248,116,49);'>" + str + "</a></html>");
                Heading3.setFont(new Font("Andy", Font.PLAIN, 20));

                Heading3.setBounds(500, 70, 600, 200);
                JPanel panel1 = new JPanel();
                JPanel panel2 = new JPanel();
                Border border1 = BorderFactory.createTitledBorder("Exercise");



                panel1.setBounds(150, 200, 1000, 190);

                String column = "Questions".toUpperCase();

                //  AttributedString as = new AttributedString(column);
                //    Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);
                //   as.addAttribute(TextAttribute.FONT, plainFont);
                //  as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON,0,8 );
                String column1 = "Problem".toUpperCase();
                String column2 = "Answer".toUpperCase();
                String column3 = "Answer".toUpperCase();
                String results = "results".toUpperCase();

                Object rowData[][] = {{"1", "3+9", "12", "12", "Correct !"},
                    {"2", "4+3", "7", "7", "Correct !"},
                    {"3", "10+8", "17", "18", "Sorry :("},
                    {"4", "5+1", "6", "6", "Correct !"},
                    {"5", "10+8", "18", "18", "Correct !"},
                    {"6", "8+2", "10", "10", "Correct !"},
                    {"7", "5+6", "4", "11", "Sorry :("}
                };
                Object columnNames[] = {column, column1, column2, column3, results};

                DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
                JTable tb1 = new JTable(model) {


                    //when checking if a cell is editable always return false



                    public boolean isCellEditable(int rowIndex, int colIndex) {
                        return false;   //Disallow the editing of any cell
                    }

                };
                //JTable table1 = new JTable(rowData, columnNames1);
                tb1.setBackground(Color.getHSBColor(153, 0, 91));


                JScrollPane scrollPane1 = new JScrollPane(tb1);
                Border raisedbevel = BorderFactory.createRaisedBevelBorder();
                Border loweredbevel = BorderFactory.createLoweredBevelBorder();
                Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
             //  ImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon");


              okbtn=new JButton("Press Ok to Exit");
                           okbtn.setBorder(raisedbevel);

              okbtn.setFont(new Font("Vani", Font.BOLD, 12));
                okbtn.setBounds(575, 390, 150, 50);
                panel2.add(scrollPane1, BorderLayout.CENTER);
                okbtn.addActionListener(new buttonActionListener());
                frame.add(Heading);
                frame.add(Heading2);
                frame.add(Heading3);
                frame.add(okbtn);
                frame.add(panel1);

                panel2.setBorder(border1);
                panel1.add(panel2);

                frame.setSize(500, 500);



                frame.setVisible(true);

            }
        };
        EventQueue.invokeLater(runner);
    }

    private static class buttonActionListener implements ActionListener {



        @Override
        public void actionPerformed(ActionEvent ae) {

        if(ae.getSource()==okbtn){
            JOptionPane.showMessageDialog(null, "All the best !!!");
            WindowEvent winClosingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING );
             Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent( winClosingEvent );

        }

        }
    }
}

preguntado el 04 de julio de 12 a las 06:07

frame.setLayout(null); Es donde comienzan los problemas.. -

Gracias Andrew por señalar mi error. salud -

2 Respuestas

Quizas

enter image description here

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Inf3rNix
 */
public class KidsProblemTable {

    private JFrame frame;
    private JPanel panel1 = new JPanel();
    private JLabel Heading = new JLabel("My Result", JLabel.CENTER);
    private JLabel Heading2 = new JLabel("Nice Job! Here are your results...", JLabel.CENTER);
    private JLabel Heading3 = new JLabel();
    private String str = "You got 5 correct answer out of 7 question";
    private JPanel panel2 = new JPanel();
    private JButton okbtn;
    private Object rowData[][] = {{"1", "3+9", "12", "12", "Correct !"},
        {"2", "4+3", "7", "7", "Correct !"},
        {"3", "10+8", "17", "18", "Sorry :("},
        {"4", "5+1", "6", "6", "Correct !"},
        {"5", "10+8", "18", "18", "Correct !"},
        {"6", "8+2", "10", "10", "Correct !"},
        {"7", "5+6", "4", "11", "Sorry :("}
    };
    private String column = "Questions".toUpperCase();
    private String column1 = "Problem".toUpperCase();
    private String column2 = "Answer".toUpperCase();
    private String column3 = "Answer".toUpperCase();
    private String results = "results".toUpperCase();
    private Object columnNames[] = {column, column1, column2, column3, results};
    private DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
    private JTable tb1 = new JTable(model) {

        private static final long serialVersionUID = 1L;

        //when checking if a cell is editable always return false
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;   //Disallow the editing of any cell
        }
    };
    private JScrollPane scrollPane1 = new JScrollPane(tb1);

    public KidsProblemTable() {
        Border border1 = BorderFactory.createTitledBorder("Exercise");
        Border raisedbevel = BorderFactory.createRaisedBevelBorder();
        tb1.setBackground(Color.getHSBColor(153, 0, 91));
        tb1.setPreferredScrollableViewportSize(tb1.getPreferredSize());
        Heading.setFont(new Font("Gabriola", Font.ITALIC, 56));
        Heading.setForeground(Color.BLUE);
        Heading2.setFont(new Font("Buxton Sketch", Font.ITALIC, 26));
        Heading2.setForeground(Color.BLUE);
        Heading3.setText("<html><a href=' ' style='color:rgb(248,116,49);'>" + str + "</a></html>");
        Heading3.setFont(new Font("Andy", Font.PLAIN, 20));
        panel1.setLayout(new GridLayout(3, 1));
        panel1.add(Heading);
        panel1.add(Heading2);
        panel1.add(Heading3);
        //  ImageIcon icon = createImageIcon("images/wavy.gif", "wavy-line border icon");
        okbtn = new JButton("     Press Ok to Exit    ");
        okbtn.setBorder(raisedbevel);
        okbtn.setFont(new Font("Vani", Font.BOLD, 12));
        okbtn.addActionListener(new buttonActionListener());
        panel2.add(okbtn);
        panel2.setBorder(border1);
        frame = new JFrame("Kids Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel1, BorderLayout.NORTH);
        frame.add(scrollPane1, BorderLayout.CENTER);
        frame.add(panel2, BorderLayout.SOUTH);
        frame.pack();
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    private class buttonActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {

            if (ae.getSource() == okbtn) {
                JOptionPane.showMessageDialog(null, "All the best !!!");
                WindowEvent winClosingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
            }
        }
    }

    public static void main(String[] args) {
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                new KidsProblemTable();
            }
        };
        EventQueue.invokeLater(runner);
    }
}

Respondido 04 Jul 12, 08:07

Gran respuesta. Lástima que el OP elija la opción (setBounds(..)) que resultará en su junto 5 preguntas. :( - Andrew Thompson

@Andrew Thompson gracias, publiqué mi respuesta a una pregunta ya aceptada: mKorbel

Encontré tu problema, agregaste el scrollPane al panel2 como

panel2.add(scrollPane1, BorderLayout.CENTER);

pero no configuró Layout como BorderLayout para panel2. y establece el diseño de panel1 como nulo, vea este código

            JPanel panel1 = new JPanel();
            panel1.setLayout(null);//add to your code
            JPanel panel2 = new JPanel();
            panel2.setLayout(new BorderLayout());//add to your code
            panel2.setBounds(0,0,800,120);//add to your code

producción:

puede que esta imagen se elimine después de algunas horas imagen de salida

Respondido 04 Jul 12, 07:07

¿Has visto la imagen que puse en mi publicación? Padman

En realidad, el borde del título del ejercicio no debe contener el botón Aceptar. Pero lo corregí en mi código. Finalmente obtuve la salida ty padman. - Craneo Rojo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.