سؤال

مرحبا الجميع.أنا أحاول تقديم البديل واجهة المستخدم الرسومية مع زر التسمية على ذلك.im باستخدام الحدود تخطيط التسمية ( في حقل الشمال ) يظهر حسنا, ولكن على زر يستغرق بقية الإطار (انها في وسط الميدان).أي فكرة عن كيفية حل هذه المشكلة ؟

هل كانت مفيدة؟

المحلول

لديك لإضافة زر لوحة أخرى، ثم قم بإضافة هذا الفريق إلى الإطار.

واتضح BorderLayout يوسع أي وقت مضى ما هو عنصر في الوسط

والتعليمات البرمجية الخاصة بك ينبغي أن تبدو هذه الآن:

قبل

public static void main( String [] args ) {
    JLabel label = new JLabel("Some info");
    JButton button = new JButton("Ok");

    JFrame frame = ... 

    frame.add( label, BorderLayout.NORTH );
    frame.add( button , BorderLayout.CENTER );
    ....

}

وتغييره إلى شيء من هذا القبيل:

public static void main( String [] args ) {
    JLabel label = new JLabel("Some info");
    JButton button = new JButton("Ok");
    JPanel panel = new JPanel();
     panel.add( button );

    JFrame frame = ... 

    frame.add( label, BorderLayout.NORTH );
    frame.add( panel , BorderLayout.CENTER);
    ....

}

وقبل / بعد

قبل http://img372.imageshack.us/img372/2860/beforedl1.png بعد http://img508.imageshack.us/img508/341/aftergq7.png

نصائح أخرى

وأو مجرد استخدام تخطيط المطلق. انها على البليت التخطيطات.

وأو تمكينه مع:

frame = new JFrame();
... //your code here

// to set absolute layout.
frame.getContentPane().setLayout(null);

وبهذه الطريقة، يمكنك وضع بحرية التحكم في أي مكان تريد.

مرة أخرى :)


    import javax.swing.*;

    public class TestFrame extends JFrame {
        public TestFrame() {
            JLabel label = new JLabel("Some info");
            JButton button = new JButton("Ok");
            Box b = new Box(BoxLayout.Y_AXIS);
            b.add(label);
            b.add(button);
            getContentPane().add(b);

        }
        public static void main(String[] args) {
            JFrame f = new TestFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);

        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top